This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
"The man who can drive himself further once the effort gets painful is the man who will win." <br />Roger Bannister
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 ]