Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 885
Anton Kraievyi Avatar asked Apr 10 '11 09:04

Anton Kraievyi


People also ask

Can we use enumeration on ArrayList?

In order to get enumeration over ArrayList with Java Collections, we use the java. util. Collections. enumeration() method.

How do you enumerate a list in Java?

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

How do I find the value of an enum in a list?

The idea is to use the Enum. GetValues() method to get an array of the enum constants' values. To get an IEnumerable<T> of all the values in the enum, call Cast<T>() on the array. To get a list, call ToList() after casting.

What is enumerated set?

An enumerated set is a finite or countable set or multiset S together with a canonical enumeration of its elements; conceptually, this is very similar to an immutable list.


2 Answers

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)); 
like image 72
WhiteFang34 Avatar answered Sep 30 '22 21:09

WhiteFang34


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

like image 40
Nate W. Avatar answered Sep 30 '22 20:09

Nate W.