How do I convert array to collection?

Category: java.util, viewed: 98806 time(s).

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 lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats