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?
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?
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.
An IDE is no substitute for an Intelligent Developer.
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