How do I convert array to collection? - JAVA Interview Questions

How do I 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


Click Here to See Answer .....
Did you like this article ?
Subscribe to my RSS feed and get more JAVA Question, and Guideline, Plus a lot more great advice to help your Software Career.

0 comments:

Related JAVA Questions Posts