Converting a Java array into Set
or List
is a very common requirement in most Java projects. Java collections frameworks supports this in the following way.
Required Imports
import java.util.List; import java.util.Set; import java.util.Arrays;
Convert Array to List
List newList = Arrays.asList(yourArray);
Convert Array to Set
Set<T> newSet = new HashSet<T>(Arrays.asList(yourArray));Note the use of generics