Below java code example shows 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) https://kodehelp.com All Rights Reserved.
****************************************************************************************/
package com.kodehelp.javautil;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
* Created by https://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