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
source
share