Listing in an ordered list or set

I want to create an ordered set or list and add enumerations to the collection so that they are inserted into the enumeration order.
Can I do this without creating an explicit comparator? Is there a collection that will use its own enumeration order to maintain order in the collection?

Groovy's example of what I want to do (I expect it to look like Java with a for loop instead of closing):

TreeSet<TransactionElement> elements = new TreeSet<TransactionElement>() 
        elementList.each{ element -> elements.add(element)}

TransactionElement is an enumeration, and the elements must be added to the TreeSet in the order in which they are listed in the enumeration

UPDATE: Solution I went with:

EnumSet<TransactionElement> elements = EnumSet.noneOf(TransactionElement.class); 
        elementList.each{ element -> elements.add(element)}

Some great examples of using EnumSet and EnumMap here

+3
source share
3

Set of enum, EnumSet, , . Set, , enum.

+7
+3

By default, the order of declaration of transfers determines the natural order of transfers. From javadoc compareTo:

Enum constants are only comparable with other enumeration constants of the same enumeration type. The natural order implemented by this method is the order in which the constants are declared.

+1
source

All Articles