Convert Array to LinkedList in Java
Last Updated :
31 Mar, 2022
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:
- Using asList() method of Collections class
- 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:
- Create an array.
- Convert the array to List.
- 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);
}
}
OutputLinkedList 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:
- Create an array.
- Create an empty LinkedList.
- 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);
}
}
OutputConverted LinkedList : [G, E, E, K, S]
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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 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
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
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 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
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read