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() + " " );
}
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.
Gowtham said:
5 years ago
Clarify it clearly.
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.
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'.
Sushma said:
9 years ago
Will any one explain this question?
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers