Filling a List with all enum values in Java

JavaListEnums

Java Problem Overview


I would like to fill a list with all possible values of an enum
Since I recently fell in love with EnumSet, I leveraged allOf()

EnumSet<Something> all = EnumSet.allOf( Something.class);
List<Something> list = new ArrayList<>( all.size());
for (Something s : all) {
	list.add( s);
}
return list;

Is there a better way (as in non obfuscated one liner) to achieve the same result?

Java Solutions


Solution 1 - Java

I wouldn't use a List in the first places as an EnumSet is more approriate but you can do

List<Something> somethingList = Arrays.asList(Something.values());

or

List<Something> somethingList =
                 new ArrayList<Something>(EnumSet.allOf(Something.class));

Solution 2 - Java

Class.getEnumConstants()

List<SOME_ENUM> enumList = Arrays.asList(SOME_ENUM.class.getEnumConstants());

Solution 3 - Java

There is a constructor for ArrayList which is

ArrayList(Collection<? extends E> c) 

Now, EnumSet extends AbstractCollection so you can just do

ArrayList<Something> all = new ArrayList<Something>(enumSet)

Solution 4 - Java

try

enum E {
	E1, E2, E3
}

public static void main(String[] args) throws Exception {
	List<E> list = Arrays.asList(E.values());
	System.out.println(list);
}

Solution 5 - Java

List<Something> result = new ArrayList<Something>(all);

EnumSet is a Java Collection, as it implements the Set interface:

public interface Set<E> extends Collection<E> 

So anything you can do with a Collection you can do with an EnumSet.

Solution 6 - Java

This is a bit more readable:

Object[] allValues = all.getDeclaringClass().getEnumConstants();

Solution 7 - Java

Try this:

... = new ArrayList<Something>(EnumSet.allOf(Something.class));

as ArrayList has a constructor with Collection<? extends E>. But use this method only if you really want to use EnumSet.

All enums have access to the method values(). It returns an array of all enum values:

... = Arrays.asList(Something.values());

Solution 8 - Java

You can use also:

Collections.singletonList(Something.values())

Solution 9 - Java

private ComboBox gender;
private enum Selgender{Male,Famle};
ObservableList<Object> observableList  =FXCollections.observableArrayList(Selgender.values());
    

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMonoThreadedView Question on Stackoverflow
Solution 1 - JavaPeter LawreyView Answer on Stackoverflow
Solution 2 - JavaAZ_View Answer on Stackoverflow
Solution 3 - JavaJackView Answer on Stackoverflow
Solution 4 - JavaEvgeniy DorofeevView Answer on Stackoverflow
Solution 5 - JavaplanetjonesView Answer on Stackoverflow
Solution 6 - JavaBlackVegetableView Answer on Stackoverflow
Solution 7 - JavaJoshuaView Answer on Stackoverflow
Solution 8 - JavaToperaView Answer on Stackoverflow
Solution 9 - Javaسيدا الشايقيView Answer on Stackoverflow