File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dan Exam Q on Collections Doubt?

 
Vishy Karl
Ranch Hand
Posts: 116
  • 0
  • 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
  • 0
  • 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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic