wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enums in Treeset Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Enums in Treeset" Watch "Enums in Treeset" New topic
Author

Enums in Treeset

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509


here how is the sorting done?
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
TreeSet will use the natural ordering of the elements in this case (since you are not passing a Comparator to the TreeSet constructor.) And I think the natural ordering of enums is the order in which the enum constants are declared in the body of the enum. You could try and change the order of declaration in the enum and run it again to confirm.


All code in my posts, unless a source is explicitly mentioned, is my own.
Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 180
Yes I tried this. it is getting the order in which enums are declared in the enum body.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Yes. same here. We shall wait for Punit or Ankit to confirm.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Abhi vijay wrote: We shall wait for Punit or Ankit to confirm.

why?


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Rafael Angarita
Ranch Hand

Joined: Jan 09, 2009
Posts: 67
compareTo

public final int compareTo(E o)

Compares this enum with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. Enum constants are only comparable to other enum constants of the same enum type. The natural order implemented by this method is the order in which the constants are declared.

Specified by:
compareTo in interface Comparable<E extends Enum<E>>

Parameters:
o - the object to be compared.
Returns:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.



From: http://java.sun.com/javase/6/docs/api/java/lang/Enum.html


Rafael Angarita.
SCJP 6.
 
jQuery in Action, 2nd edition
 
subject: Enums in Treeset