+ 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?
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."
+ 6
Only unique values are stored on a set, not duplicate values.