A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/convert-list-to-array-in-java/ below:

Convert List to Array in Java

Convert List to Array in Java

Last Updated : 11 Jul, 2025

The List interface provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects where duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. Now here we are given a List be it any LinkedList or ArrayList of strings, our motive is to convert this list to an array of strings in Java using different methods. 

Methods to Convert List to Array in Java
  1. Using get() method
  2. Using toArray() method
  3. Using Stream introduced in Java 8
Method 1: Using get() method

We can use the below list method to get all elements one by one and insert them into an array.

Return Type: The element at the specified index in the list.

Syntax: 

public E get(int index)

Example:

Java
// Java program to Convert a List to an Array
// Using get() method in a loop

// Importing required classes
import java.io.*;
import java.util.LinkedList;
import java.util.List;

// Main class
class GFG {

    // Main driver method
    public static void main(String[] args)
    {

        // Creating a LinkedList of string type by
        // declaring object of List
        List<String> list = new LinkedList<String>();

        // Adding custom element to LinkedList
        // using add() method
        list.add("Geeks");
        list.add("for");
        list.add("Geeks");
        list.add("Practice");

        // Storing it inside array of strings
        String[] arr = new String[list.size()];

        // Converting ArrayList to Array
        // using get() method
        for (int i = 0; i < list.size(); i++)
            arr[i] = list.get(i);

        // Printing elements of array on console
        for (String x : arr)
            System.out.print(x + " ");
    }
}

Output
Geeks for Geeks Practice 
Explanation of the Program: Time and Space complexities: Method 2: Using toArray() method

The toArray() method without any arguments returns an array containing all of the elements in the list in the proper sequence . The runtime type of the returned array is Object[].

Syntax:

Without Arguments:

public Object[] toArray()

With Array Argument:

public <T> T[] toArray(T[] a)

Example:

Java
// Java Program to Convert a List to an array
// using toArray() Within a loop

// Importing utility classes
import java.util.*;

// Main class
public class GFG {

    // Main driver method
    public static void main(String[] args)
    {

        // Creating an empty LinkedList of string type
        // by declaring object of List
        List<String> list = new LinkedList<String>();

        // Adding elements to above LinkedList
        // using add() method
        list.add("Geeks");
        list.add("for");
        list.add("Geeks");
        list.add("Practice");

        // Converting List to array
        // using toArray() method
        String[] arr = list.toArray(new String[0]);

        // Printing elements of array
        // using for-each loop
        for (String x : arr)
            System.out.print(x + " ");
    }
}

Output
Geeks for Geeks Practice 
Explanation of the Program: Time and Space complexities: Method 3: Using Stream introduced in Java8

The Streams allow functional-style operations on sequences of the elements. The toArray() method of the Stream interface can be used to convert the elements of the stream into an array. The Stream.toArray(IntFunction<A[]> generator) method returns an array containing the elements of this stream.

Syntax:

public <A> A[] toArray(IntFunction<A[]> generator)

Example:

Java
// Java Program to Demonstrate conversion of List to Array
// Using stream

// Importing utility classes
import java.util.*;

// Main class
class GFG {

    // Main driver method
    public static void main(String[] args)
    {

        // Creating an empty LinkedList of string type
        List<String> list = new LinkedList<String>();

        // Adding elements to above LinkedList
        // using add() method
        list.add("Geeks");
        list.add("for");
        list.add("Geeks");
        list.add("Practice");

        // Storing size of List
        int n = list.size();

        // Converting List to array via scope resolution
        // operator using streams
        String[] arr
            = list.stream().toArray(String[] ::new);

        // Printing elements of array
        // using enhanced for loop
        for (String x : arr)
            System.out.print(x + " ");
    }
}

Output
Geeks for Geeks Practice 
Explanation of the Program: Time and Space complexities:

Tip: We can convert the array back to the list via asList() method.  

Related Articles:  



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4