Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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:
How to Convert ArrayList to LinkedHashSet in Java?
Next article icon

Convert Array to LinkedList in Java

Last Updated : 31 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Array is contiguous memory allocation while LinkedList is a block of elements randomly placed in the memory which are linked together where a block is holding the address of another block in memory. Sometimes as per requirement or because of space issues in memory where there are bigger chunks of code in the enterprising world it becomes necessary to convert arrays to List. Here conversion of array to LinkedList is demonstrated. 

Methods: 

  1. Using asList() method of Collections class
  2. Using addAll() method of Collections class

Method 1: Using asList() method of Collections class

This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray(). The returned list is serialized and implements RandomAccess. This runs in O(1) time. 

Syntax: 

public static List asList(T... a) ;

Parameters: This method takes the array a which is required to be converted into a List. Here array of parameters works similar to an object array parameter.

Approach:

  1. Create an array.
  2. Convert the array to List.
  3. Create LinkedList from the List using the constructor.

Example

Java
// Java Program to convert Array to LinkedList  // Importing array, List & LinkedList classes from // java.util package import java.util.Arrays; import java.util.LinkedList; import java.util.List;  // Class public class GFG {      // Main driver method     public static void main(String[] args)     {          // Create a string Array         String[] strArr = { "A", "B", "C", "D", "E" };          // Convert array to list         List<String> list = Arrays.asList(strArr);          // Create a LinkedList and         // pass List in LinkedList constructor         LinkedList<String> linkedList             = new LinkedList<String>(list);          // Display and print all elements of LinkedList         System.out.println("LinkedList of above array : "                            + linkedList);     } } 

Output
LinkedList of above array : [A, B, C, D, E]

Method 2: Using addAll() method of Collections class

This method is used to append all the elements from the collection passed as parameter to this function to the end of a list keeping in mind the order of return by the collections iterator. 

Syntax: 

boolean addAll(Collection C) ;

Parameters: The parameter C is a collection of ArrayList. It is the collection whose elements are needed to be appended at the end of the list.

Return Value: The method returns true if at least one action of append is performed else return false.

Approach: 

  1. Create an array.
  2. Create an empty LinkedList.
  3. Use addAll() method of collections class which takes two objects as parameters.
    • First object as where to be converted
    • Second object as which to be converted.

Example  

Java
// Java Program to convert Array to LinkedList  // Importing array, List & LinkedList, Collections classes // from java.util package import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.List;  // Class public class GFG {      // Main driver method     public static void main(String[] args)     {          // Create an Array         // here string type         String[] strArr = { "G", "E", "E", "K", "S" };          // Create an empty LinkedList         LinkedList<String> linkedList             = new LinkedList<String>();          // Append all elements of array to linked list         // using Collections.addAll() method         Collections.addAll(linkedList, strArr);          // Print the above LinkedList received         System.out.println("Converted LinkedList : "+linkedList);     } } 

Output
Converted LinkedList : [G, E, E, K, S]


 


Next Article
How to Convert ArrayList to LinkedHashSet in Java?
author
mukulsomukesh
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Java Programs
  • Technical Scripter 2020
  • Java-Collections
  • Java-Arrays
  • java-LinkedList
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

  • Convert a HashSet to a LinkedList in Java
    In Java, HashSet and LinkedList are linear data structures. These are majorly used in many cases. There may be some cases where we need to convert HashSet to LinkedList. So, in this article, we will see how to convert HashSet to LinkedList in Java. Java Program to Convert a HashSet to a LinkedList T
    2 min read
  • Convert HashMap to LinkedList in Java
    HashMap is similar to the HashTable, but it is unsynchronized. It allows to store the null keys as well, but there should be only one null key object and there can be any number of null values. LinkedList is a part of the Collection framework present in java.util package. This class is an implementa
    2 min read
  • How to Convert ArrayList to LinkedHashSet in Java?
    ArrayList is a data structure that overcomes the shortcomings of the common array in Java wherein the size has to be explicitly specified beforehand. The length of the array data structure cannot be modified which is taken care of the ArrayList data structure. This data structure is also known as th
    8 min read
  • How to Sort a LinkedList in Java?
    A Linked List is a linear data structure, in which the elements are not stored at contiguous memory locations. Sorting the nodes of a Singly Linked list in ascending order: We can sort the LinkedList by many sorting techniques: Selection SortInsertion sortQuick sortMerge sort Method 1: Sort Linked L
    13 min read
  • How to Convert LinkedHashMap to List in Java?
    LinkedHashMap is predefined class in Java which is similar to HashMap, contains key and its respective value unlike HashMap, in LinkedHashMap insertion order is preserved. We to convert LinkedHashMap to ArrayList. A Map store data in pair of Key and Value while converting a LinkedHashMAp to ArrayLis
    2 min read
  • How to Convert a LinkedHashSet into an Array and List in Java?
    In Java, LinkedHashSet is a predefined class of Java that extends the HashSet and it implements the Set interface. It can be used to implement the doubly linked list of the elements in the set, and it provides the predictable iteration order. In this article, we will learn how to convert a LinkedHas
    3 min read
  • Java Program to Convert an Array into a List
    In Java, arrays and lists are two commonly used data structures. While arrays have a fixed size and are simple to use, lists are dynamic and provide more flexibility. There are times when you may need to convert an array into a list, for instance, when you want to perform operations like adding or r
    4 min read
  • Program to convert Array to Set in Java
    Array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array. In the case of primitives data types, the actual values are stored in contiguous memory locations. In cas
    7 min read
  • How to Clone a List in Java?
    Given a list in Java, the task is to clone this list. Example: Input: list = ["Geeks", "for", "Geeks"] Output: clonedList = ["Geeks", "for", "Geeks"] Input: list = ["GeeksForGeeks", "A Computer Science", "Portal"] Output: clonedList = ["GeeksForGeeks", "A Computer Science", "Portal"] In Java, there
    9 min read
  • How to Convert all LinkedHashMap Values to a List in Java?
    The task is to convert all LinkedHashMap values to a list in java. LinkedHashMap is an implementation of a Map. The Map and List are two different data structures. The Map stores key-value pairs while the List is an ordered collection of elements. To convert all values of the LinkedHashMap to a List
    2 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