Convert Enumeration to a Set/List
You can use Collections.list()
to convert an Enumeration
to a List
in one line:
List<T> list = Collections.list(enumeration);
There's no similar method to get a Set
, however you can still do it one line:
Set<T> set = new HashSet<T>(Collections.list(enumeration));
How about this: Collections.list(Enumeration e) returns an ArrayList<T>
If you need Set
rather than List
, you can use EnumSet.allOf().
Set<EnumerationClass> set = EnumSet.allOf(EnumerationClass.class);
Update: JakeRobb is right. My answer is about java.lang.Enum instead of java.util.Enumeration. Sorry for unrelated answer.
When using guava (See doc) there is Iterators.forEnumeration
. Given an Enumeration x
you can do the following:
to get a immutable Set:
ImmutableSet.copyOf(Iterators.forEnumeration(x));
to get a immutable List:
ImmutableList.copyOf(Iterators.forEnumeration(x));
to get a hashSet:
Sets.newHashSet(Iterators.forEnumeration(x));