How to convert an array into a collection object?

DeeKay October 6, 2011 at 7:33 pm · java.util

Below java code example show how to convert an array into a Collection object. To convert array based data into list / collection, we can use java.util.Arrays class. This class provide a static method asList(Object[] a) that converts array into list / collection.

/****************************************************************************************
 * Created on 10-2011Copyright(c) http://kodehelp.com All Rights Reserved.
 ****************************************************************************************/
package com.kodehelp.javautil;

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

/**
 * Created by http://kodehelp.com
 * Date: 10/6/11
 */
public class ArrayToCollection {
    public static void main(String args[]){
        String[] array = {"TOM", "JACK", "JILL", "JANE"};
        List list = Arrays.asList(array);

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

OUTPUT:


TOM
JACK
JILL
JANE