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
An enum is just another class in Java, it should be possible.
More accurately, an enum is an instance of Object: http://docs.oracle.com/javase/6/docs/api/java/lang/Enum.html
So yes, it should work.
You can simply write
new ArrayList<MyEnum>(Arrays.asList(MyEnum.values()));
It is possible but you should use EnumSet instead
enum MyEnum {
ONE, TWO;
public static final EnumSet<MyEnum> all = EnumSet.of(ONE, TWO);
}
This is a more generic solution, that can be use for any Enum object, so be free of used.
static public List<Object> constFromEnumToList(Class enumType) {
List<Object> nueva = new ArrayList<Object>();
if (enumType.isEnum()) {
try {
Class<?> cls = Class.forName(enumType.getCanonicalName());
Object[] consts = cls.getEnumConstants();
nueva.addAll(Arrays.asList(consts));
} catch (ClassNotFoundException e) {
System.out.println("No se localizo la clase");
}
}
return nueva;
}
Now you must call this way:
constFromEnumToList(MiEnum.class);
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()
Yes it is definitely possible, but you will have to do
List<MyEnum> al = new ArrayList<MyEnum>();
You can then add elements to al
: al.add(ONE)
or al.add(TWO)
.