• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

TreeSet to HashMap  RSS feed

 
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have  two TreeSet as h1 and h2 now i want to convert these two TreeSet into HashMap ,h1 as a key and and h2 as a value. So please tell me how can i do this ? Also if i pass (2,3) and(3,2) is it considered duplicate or not?
 
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With Maps you can have duplicate values but keys must be unique. Non-unique keys will overwrite the original entry with the same key.
 
Marshal
Posts: 64086
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you calling the sets h1 and h2? Those are really confusing names. Your other variable names are hardly better.
Look through the Java™ Tutorials, where you will find that the Map interface represents the mathematical concept of a function, which is a different way of saying what Carey said, that keys cannnot be duplicated. The ordered pairs (2, 3) and (3, 2) count as different.
Yes, you can have a Map<Set<XYZ>, Set<ABC>> if you want to. But why?
 
Saloon Keeper
Posts: 20635
122
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's probably not a good idea to name two TreeSets and one HashMap as "h1, h2, and h3". Kind of confusing. Also, you're not actually using the HashMap that I can see.

It appears that your input stream consists of pairs of integers, one to be used in the first TreeSet and one to be used in the second Treeset. Perhaps you're intending at some point to add them as key and value in your HashMap?

To amplify Cary's comment, if you attempt to add a value to a Set and that value is already a member of the Set, the default reaction of the Set class is to simply silently ignore the second and successive attempts to add that value. That can easily lead to a situation where you might end up with, say, 47 values in the first TreeSet and 53 values in the second one when you actually had read in 62 pairs of values from the scanner.

Futhermore, a TreeSet is traverse as an ordered collection. Even if you did have an equal numbers loaded into both TreeSets, the traversal of those TreeSets will not be in the order that the pairs were read in, but instead in ascending value of the individual values. So, for example, reading:
would read out the first TreeSet as (1,4,6,7) and the second TreeSet as (2,3,5,7), breaking the original associations beyond all hope of recovery.

And all of that is even before what Carey pointed out. Since you're using Set objects, there will be no possibility of pulling a duplicate key value out of one, but if you did, the data value that you attached to that key would wipe out any earlier data value for that key value.

 
My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!