This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes TreeSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "TreeSet" Watch "TreeSet" New topic


Shravan Durvasula

Joined: Mar 06, 2004
Posts: 19
Why is it that the following code does not run?

Thanks in advance,
Fletcher Estes
Ranch Hand

Joined: Jul 01, 2004
Posts: 108
Hi Shravan,

A TreeSet must keep all its elements in order, but unless you specify how to order the elements, it's going to run into problems! What this means is that either
  • Any object you add to the TreeSet implements the Comparable interface. To implement this interface, your objects must contain a compareTo(Object o) method.
  • Or, when you initialise your TreeSet, pass a Comparator object. This Comparator has a compare(Object o1, Object o2) method that you should override.

  • Read more about the TreeSet on Sun's website.
    Shravan Durvasula

    Joined: Mar 06, 2004
    Posts: 19
    Thanks Fletcher. That helped.
    jQuery in Action, 2nd edition
    subject: TreeSet
    Similar Threads
    [imp] how can we sort the objects in Collections
    Sort Order, Duplicacy and ClassCastException in TreeSet
    TreeSet behavior
    Generic TreeSet