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 in which 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:
- Using get() method
- Using toArray() method
- 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 + " " );     } } |
Geeks for Geeks Practice
Â
The time complexity and space complexity of this Java code is O(n), where n is the size of the list.
Method 2: Using toArray() method
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 + " " );     } } |
Geeks for Geeks Practice
Â
The time complexity and space complexity of this Java code is O(n), where n is the size of the list.
Method 3: Using Stream introduced in Java8
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 + " " );     } } |
Geeks for Geeks Practice
Â
Tip: We can convert the array back to the list via asList() method. Â
The time complexity and space complexity of this Java code is O(n), where n is the size of the list.
Related Articles:Â Â