Tuesday, April 17, 2012

Java:: How to convert Arrays to List and Set (Collection) in 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

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

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:


Related Posts Plugin for WordPress, Blogger...