Convert List to Array

      No Comments on Convert List to Array




The Java.util.List is a child interface of Collection. It maintains the order of the objects insertion in the List. Duplicate values can also be stored in List.

A List of Objects can be converted into array in different ways.

The below are different methods using Which a list can be converted into an array.

Conversion of List to Array Using List Methods:

The below functions are part of java.util.List class.

(1.) Object[] java.util.List.toArray()

It returns an array containing all of the elements in a list in proper sequence (from first to last element). The return type of this method is Object array.

(2.) T[] toArray(T[] a)

It Returns an array containing all of the elements in a list in proper sequence (from first to last element). The return type of this array is the same as that which is passed as an argument.

Conversion of List to Array Using Stream Methods:

The below functions are part of java.util.stream.Stream class.

(1) Object[] java.util.stream.Stream.toArray();

It returns an array containing the elements of this stream.

(2) A[] toArray(IntFunction<A[]> generator);

It Returns an array containing the elements of a stream. The return type of this array is the same as that which is passed as an argument.