Is it possible to create ArrayList of enum values (and manipulate it)? For example:
enum MyEnum { ONE, TWO } MyEnum my = MyEnum.ONE; List <?> al = new ArrayList <?>(); al.add(my); al.remove(al.size()-1);
The idea is to use the Enum. GetValues() method to get an array of the enum constants' values. To get an IEnumerable<T> of all the values in the enum, call Cast<T>() on the array. To get a list, call ToList() after casting.
Yes. If you do not care about the order, use EnumSet , an implementation of Set .
You can create a list that holds enum instances just like you would create a list that holds any other kind of objects: ? List<ExampleEnum> list = new ArrayList<ExampleEnum>(); list.
You can simply write
new ArrayList<MyEnum>(Arrays.asList(MyEnum.values()));
Can you make and edit a collection of objects from an enum? Yes.
If you do not care about the order, use EnumSet
, an implementation of Set
.
enum Animal{ DOG , CAT , BIRD , BAT ; } Set<Animal> flyingAnimals = EnumSet.of( BIRD , BAT ); Set<Animal> featheredFlyingAnimals = flyingAnimals.clone().remove( BAT ) ;
If you care about order, use a List
implementation such as ArrayList
. For example, we can create a list of a person’s preference in choosing a pet, in the order of their most preferred.
List< Animal > favoritePets = new ArrayList<>() ; favoritePets.add( CAT ) ; // This person prefers cats over dogs… favoritePets.add( DOG ) ; // …but would accept either. // This person would not accept a bird nor a bat.
For a non-modifiable ordered list, use List.of
.
List< Animal > favoritePets = List.of( CAT , DOG ) ; // This person prefers cats over dogs, but would accept either. This person would not accept a bird nor a bat.
The Answer (EnumSet
) by Amit Deshpande and the Answer (.values
) by Marko Topolnik are both correct. Here is a bit more info.
Enum.values
The .values()
method is an implicitly declared method on Enum
, added by the compiler. It produces a crude array rather than a Collection
. Certainly usable.
Special note about documentation: Being unusual as an implicitly declared method, the .values()
method is not listed among the methods on the Enum
class. The method is defined in the Java Language Specification, and is mentioned in the doc for Enum.valueOf
.
EnumSet
– Fast & SmallThe upsides to EnumSet
include:
To quote the class doc:
Enum sets are represented internally as bit vectors. This representation is extremely compact and efficient. The space and time performance of this class should be good enough to allow its use as a high-quality, typesafe alternative to traditional int-based "bit flags." Even bulk operations (such as containsAll and retainAll) should run very quickly if their argument is also an enum set.
Given this enum:
enum Animal { DOG , CAT , BIRD , BAT ; }
Make an EnumSet
in one line.
Set<Animal> allAnimals = EnumSet.allOf( Animal.class );
Dump to console.
System.out.println( "allAnimals : " + allAnimals );
allAnimals : [DOG, CAT, BIRD, BAT]
Make a set from a subset of the enum objects.
Set<Animal> flyingAnimals = EnumSet.of( BIRD , BAT );
Look at the class doc to see many ways to manipulate the collection including adding or removing elements.
Set<Animal> featheredFlyingAnimals = EnumSet.copyOf( flyingAnimals ).remove( BAT );
The doc promises the Iterator for EnumSet is in natural order, the order in which the values of the enum were originally declared.
To quote the class doc:
The iterator returned by the iterator method traverses the elements in their natural order (the order in which the enum constants are declared).
Frankly, given this promise, I'm confused why this is not a SortedSet
. But, oh well, good enough. We can create a List
from the Set
if desired. Pass any Collection
to constructor of ArrayList
and that collection’s Iterator
is automatically called on your behalf.
List<Animal> list = new ArrayList<>( allAnimals );
Dump to console.
System.out.println("list : " + list );
When run.
list : [DOG, CAT, BIRD, BAT]
In Java 10 and later, you can conveniently create a non-modifiable List
by passing the EnumSet
. The order of the new list will be in the iterator order of the EnumSet
. The iterator order of an EnumSet
is the order in which the element objects of the enum were defined on that enum.
List< Animal > nonModList = List.copyOf( allAnimals ) ; // Or pass Animals.values()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With