102

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?

1
  • ArrayList<Something> list = new ArrayList<Something>(Arrays.asList(Something.values()))
    – TacB0sS
    Dec 25, 2014 at 9:41

9 Answers 9

204

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));
5
  • 3
    Sometimes EnumSet don't cut it. In particular when order matters Apr 22, 2013 at 14:06
  • 1
    @MonoThreaded What order did you have in mind? An EnumSet is in the natural order for an enum. The list will be in the same order as the set, so using a List doesn't change anything here. Apr 22, 2013 at 14:07
  • 1
    Sorry, ordering is out of scope for this question. I believe you meant values() in the first example. Apr 22, 2013 at 14:17
  • @PeterLawrey we use Enums all the time. When we design a workflow the approval process may not move in the Natural Order of the Enum, this is why a List<Enum> comes in handy vs an EnumSet. Feb 19, 2014 at 15:19
  • EnumSet does not have a get() method (easy random access) while List does. This may be important for whatever the List/EnumSet is intended to be used for
    – pallgeuer
    Jan 4, 2021 at 18:04
15

Class.getEnumConstants()

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

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)
9

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);
}
1
  • worth clarifying that you do not get a java.util.ArrayList out of this, as stated in the question Apr 22, 2013 at 14:15
1
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.

0

This is a bit more readable:

Object[] allValues = all.getDeclaringClass().getEnumConstants();
2
  • 1
    Then you could simply do Something.class.getEnumConstants() right?
    – shyam
    Apr 22, 2013 at 14:05
  • 2
    ... or Something.values(); Apr 22, 2013 at 14:06
0

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());
0

You can use also:

Collections.singletonList(Something.values())
1
  • I am getting hexCode Apr 27, 2020 at 9:39
-3
private ComboBox gender;
private enum Selgender{Male,Famle};
ObservableList<Object> observableList  =FXCollections.observableArrayList(Selgender.values());
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.