Convert List to Array in Java Last Updated : 17 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 JavaUsing get() methodUsing toArray() methodUsing Stream introduced in Java 8Method 1: Using get() methodWe 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 + " "); } } OutputGeeks for Geeks Practice Explanation of the Program:This Java program demonstrates how to convert a LinkedList of strings to an array using the get() method in a loop. It creates a LinkedList, adds elements to it, then iterates through the list to copy each element into an array. Finally, it prints the array elements to the console.Time and Space complexities:Time Complexity: O(n), where n is the size of the list.Space Complexity: O(n), where n is the size of the list.Method 2: Using toArray() methodThe 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 + " "); } } OutputGeeks for Geeks Practice Explanation of the Program:This Java program illustrates converting a LinkedList of strings to an array using the toArray() method. It creates a LinkedList, adds elements to it, and then converts the list to an array with toArray(new String[0]). Finally, it prints the array elements using a for-each loop.Time and Space complexities:Time Complexity: O(n), where n is the size of the list.Space Complexity: O(n), where n is the size of the list.Method 3: Using Stream introduced in Java8The 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 + " "); } } OutputGeeks for Geeks Practice Explanation of the Program:This Java program demonstrates converting a LinkedList of strings to an array using streams. It creates a LinkedList, adds elements, and converts the list to an array with list.stream().toArray(String[]::new). Finally, it prints the array elements using an enhanced for loop.Time and Space complexities:Time Complexity: O(n), where n is the size of the list.Space Complexity: O(n), where n is the size of the list.Tip: We can convert the array back to the list via asList() method. Related Articles: ArrayList to Array Conversion in JavaSet to Array in Java Comment More infoAdvertise with us Next Article Convert List to Array in Java GeeksforGeeks Improve Article Tags : Misc Java Java-Arrays java-list Practice Tags : JavaMisc Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e 7 min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read TCP/IP Model The TCP/IP model is a framework that is used to model the communication in a network. It is mainly a collection of network protocols and organization of these protocols in different layers for modeling the network.It has four layers, Application, Transport, Network/Internet and Network Access.While 7 min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br 14 min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Like