java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to convert array to collection?

To convert array based data into list / collection based we can use java.util.Arrays class. This class provide a static method asList(Object[] a) that converts array into list / collection.

import java.util.Arrays;
import java.util.List;
import java.util.Iterator;

public class ArraysExample
{
    public static void main(String[] args)
    {
        String[] array = {"Happy", "New", "Year", "2006"};
        List list = Arrays.asList(array);

        Iterator iterator = list.iterator();
        while (iterator.hasNext())
        {
            System.out.println((String) iterator.next());
        }
    }
}

The result of our code is:

Happy
New
Year
2006