Search code examples
javacollectionsconventions

Convert Enumeration to a Set/List


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


Solution

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