1. Posts/

Java:: How to convert Arrays to List and Set (Collection) in Java

···
java

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
#

1
2
3
import java.util.List;
import java.util.Set;
import java.util.Arrays;

Convert Array to List
#

1
List newList = Arrays.asList(yourArray);

Convert Array to Set
#

1
Set<T> newSet = new HashSet<T>(Arrays.asList(yourArray));

Note the use of generics

Difference between List and Set
#

List is an ordered sequence of elements whereas Set is a distinct list of elements which is unordered

Read more here: