File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan Exam Q on Collections Doubt? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Exam Q on Collections Doubt?" Watch "Dan Exam Q on Collections Doubt?" New topic

Dan Exam Q on Collections Doubt?

Vishy Karl
Ranch Hand

Joined: Sep 08, 2003
Posts: 116
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,

"The man who can drive himself further once the effort gets painful is the man who will win." <br />Roger Bannister
Yoo-Jin Lee
Ranch Hand

Joined: Nov 01, 2000
Posts: 119
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?
[ December 15, 2003: Message edited by: Yoo-Jin Lee ]
I agree. Here's the link:
subject: Dan Exam Q on Collections Doubt?
It's not a secret anymore!