Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Convert List to Array in Java
Next article icon

Convert List to Array in Java

Last Updated : 17 Jul, 2024
Comments
Improve
Suggest changes
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 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:

  • 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() 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:

  • 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 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:

  • 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 Java
  • Set to Array in Java

Next Article
Convert List to Array in Java
https://media.geeksforgeeks.org/auth/avatar.png
GeeksforGeeks
Improve
Article Tags :
  • Misc
  • Java
  • Java-Arrays
  • java-list
Practice Tags :
  • Java
  • Misc

Similar Reads

    How to convert LinkedList to Array in Java?
    Given a Linked List in Java, the task is to convert this LinkedList to Array. Examples: Input: LinkedList: ['G', 'e', 'e', 'k', 's'] Output: Array: ['G', 'e', 'e', 'k', 's'] Input: LinkedList: [1, 2, 3, 4, 5] Output: Array: [1, 2, 3, 4, 5] Approach: Get the LinkedListConvert the LinkedList to Object
    2 min read
    Convert Vector to ArrayList in Java
    There are multiple ways to convert vector to ArrayList, using passing the Vector in ArrayList constructor and by using simple vector traversal and adding values to ArrayList. Approach 1: Create a Vector.Add some values in Vector.Create a new ArrayList.Traverse vector from the left side to the right
    3 min read
    How to Convert Vector to Array in Java?
    As we all know an array is a group of liked-typed variables that are referred to by a common name while on the other hand vectors basically fall in legacy classes but now it is fully compatible with collections. It is found in java.util package and implement the List interface which gives a superior
    3 min read
    Set to Array in Java
    Given a set (HashSet or TreeSet) of strings in Java, convert it into an array of strings. Input : Set hash_Set = new HashSet(); hash_Set.add("Geeks"); hash_Set.add("For"); Output : String arr[] = {"Geeks", "for"} Method 1 (Simple) We simply create an empty array. We traverse the given set and one by
    3 min read
    Array to Stream in Java
    Prerequisite : Stream In Java Using Arrays.stream() : Syntax : public static <T> Stream<T> getStream(T[] arr) { return Arrays.stream(arr); } where, T represents generic type. Example 1 : Arrays.stream() to convert string array to stream. Java // Java code for converting string array // t
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences