aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collections question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collections question" Watch "Collections question" New topic
Author

Collections question

Sriram Chintapalli
Ranch Hand

Joined: Dec 16, 2003
Posts: 59
Hi all,
In a TreeSet/TreeMap are elements(keys) stored in sorted order or stored randomly and retrieved in sorted order?
thanks,
Sriram
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
From the API Spec for TreeSet:

This class implements the Set interface, backed by a TreeMap instance. This class guarantees that the sorted set will be in ascending element order, sorted according to the natural order of the elements (see Comparable), or by the comparator provided at set creation time, depending on which constructor is used.

And from the API Spec for TreeMap:

Red-Black tree based implementation of the SortedMap interface. This class guarantees that the map will be in ascending key order, sorted according to the natural order for the key's class (see Comparable), or by the comparator provided at creation time, depending on which constructor is used.

So, in this case, it appears that the set or set of keys is stored and retrieved in a sorted order.


SCJP Tipline, etc.
Sriram Chintapalli
Ranch Hand

Joined: Dec 16, 2003
Posts: 59
Thanks for the explanation Corey.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Collections question