Why the answere is 169 not 1996? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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 Respostas
+ 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