Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

TreeSet to HashMap  RSS feed

Ranch Hand
Posts: 36
  • 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?
Saloon Keeper
Posts: 5478
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.
Posts: 62863
  • 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?
Posts: 20348
Android Eclipse IDE Linux
  • 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.

Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!