Convert Enumeration to a Set/List

JavaCollectionsConventions

Java Problem Overview


Is there some one-liner bridge method to dump a given Enumeration to java.util.List or java.util.Set?

Something built-in like Arrays.asList() or Collection.toArray() should exist somewhere, but I'm unable to find that in my IntelliJ debugger's evaluator window (and Google/SO results, too).

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

How about this: Collections.list(Enumeration e) returns an ArrayList<T>

Solution 3 - Java

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.

Solution 4 - Java

There is a simple example of convert enumeration to list. for this i used Collections.list(enum) method.

public class EnumerationToList {
 
    public static void main(String[] args) {
        Vector<String> vt = new Vector<String>();
        vt.add("java");
        vt.add("php");
        vt.add("array");
        vt.add("string");
        vt.add("c");

        Enumeration<String> enm = vt.elements();
        List<String> ll = Collections.list(enm);
        System.out.println("List elements: " + ll);
    }

}

Reference : How to convert enumeration to list

Solution 5 - Java

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

Solution 6 - Java

There's also in Apache commons-collections EnumerationUtils.toList(enumeration)

Solution 7 - Java

I needed same thing and this solution work fine, hope it can help someone also

Enumeration[] array = Enumeration.values();
List<Enumeration> list = Arrays.asList(array);

then you can get the .name() of your enumeration.

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
QuestionAnton KraievyiView Question on Stackoverflow
Solution 1 - JavaWhiteFang34View Answer on Stackoverflow
Solution 2 - JavaNate W.View Answer on Stackoverflow
Solution 3 - JavaTimur LevadnyView Answer on Stackoverflow
Solution 4 - JavaAnuj DhimanView Answer on Stackoverflow
Solution 5 - JavaAlexander OhView Answer on Stackoverflow
Solution 6 - JavaGabiMView Answer on Stackoverflow
Solution 7 - JavaS_intgView Answer on Stackoverflow