java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to convert collection into array?

To convert collection-based data into array we can use toArray() method provided by the implementation of Collection interface such as java.util.ArrayList.

import java.util.List;
import java.util.ArrayList;

public class CollectionToArrayExample
{
    public static void main(String[] args)
    {
        List list = new ArrayList();
        list.add("Kode");
        list.add("Java");
        list.add("Sample");
        list.add("Code");

        Object[] array = list.toArray();
        for (int i = 0; i < array.length; i++)
        {
            System.out.println(array[i].toString());
        }
    }
}

Our sample code result is shown below:

Kode
Java
Sample
Code