Java Programming - Objects and Collections - Discussion

Discussion Forum : Objects and Collections - Finding the output (Q.No. 8)
8.
What will be the output of the program?
TreeSet map = new TreeSet();
map.add("one");
map.add("two");
map.add("three");
map.add("four");
map.add("one");
Iterator it = map.iterator();
while (it.hasNext() ) 
{
    System.out.print( it.next() + " " );
}
one two three four
four three two one
four one three two
one two three four one
Answer: Option
Explanation:

TreeSet assures no duplicate entries; also, when it is accessed it will return elements in natural order, which typically means alphabetical.

Discussion:
5 comments Page 1 of 1.

Stan said:   9 years ago
If TreeSet assures not duplication, then, we should have them printed in the natural order. In that case, it should be A.

Sushma said:   9 years ago
Will any one explain this question?

Gayan said:   9 years ago
TreeSet has no duplicates. So 4th answer give up when TreeSet uses it comes natural order, natural order mean alphabetical order. Then the Answer is 'C'.

Vivekanand kumar said:   8 years ago
In alphabetical order 'f' comes first then after 'o' then 'th' (here t are two times so we will go to next character and nest character is h so) "three" will come first then last two will come. That's why the answer 3rd is correct.

Gowtham said:   5 years ago
Clarify it clearly.

Post your comments here:

Your comments will be displayed after verification.