This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Dan Exam Q on Collections Doubt?  RSS feed

 
Vishy Karl
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
THe foll. Q is in the collections section of Dan's Exam.

Answer given is
13 c TreeSet The elements are not key/value pairs; so a Map is not a good choice. A List generally accepts duplicate elements. A Set stores a collection of unique objects; so any attempt to store a duplicate object is rejected. TreeSet stores elements in an order that is determined either by a Comparator or by the Comparable interface.
But the question is that given the property
" The entries can be sorted using the Comparable interface. "
But TreeSet is already sorted . So how can it be the answer ?
Can't HashSet be given as the answer as it is not already sorted ?
Sorry if I sound confused but I am really confused,
Thanks in advance,
Bye.
 
Yoo-Jin Lee
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vishy,
The great thing about TreeSet is that you can set the Comparator. So you can base your sort on the objects you are expecting to add.
From the API:

You cannot use a HashSet because it has no internal order. That is, when you call iterator() on it, it will return your elements in no particular order.
Take a look at the API for each classes and I'm sure it'll be much more clearer.
Makes sense?
-Yoo-Jin
[ December 15, 2003: Message edited by: Yoo-Jin Lee ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!