+ 1

Why the answere is 169 not 1996?

(java.util.*is imported) TreeSet set =new TreeSet(); set.add(1); set.add(9); set.add(9); set.add(6); for(Object O : set){ System.out.println(o); } Explain this code?

29th Nov 2018, 2:27 AM
Nitin Gutte
Nitin Gutte - avatar
2 Answers
+ 2
From java 7 documentation: "A NavigableSet implementation based on a TreeMap. The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used." TreeSet automaticly sort the given element in ascending order, thats why 6 appear before 9. Where's the other 9 ? Also from documentation "add (E e): Adds the specified element to this set if it is not already present."
29th Nov 2018, 4:05 AM
Taste
Taste - avatar
+ 6
Only unique values are stored on a set, not duplicate values.
29th Nov 2018, 5:08 AM
Lambda_Driver
Lambda_Driver - avatar