LinkedList vs ArrayList in Java : Know the major differences

Last updated on Nov 27,2019 2.6K Views

LinkedList vs ArrayList in Java : Know the major differences

edureka.co

List in Java is a sub-interface of the collection interface that gives optimal solutions with concepts like positional access, iteration and so on. In this article, I will discuss the major differences between the list interface LinkedList vs ArrayList in Java.

Below are the topics covered in this article:

Let’s begin!

What is LinkedList?

After arrays, the second most popular data structure is definitely a Linked List. A linked list is a linear data structure which is constituted by a chain of nodes in which each node contains a value and a pointer to the next node in the chain. Also, the last link in a linked list points to null, indicating the end of the chain. An element in a linked list is called a node. The first node in the list is called the head. The last node is called the tail.

Let me give you a simple example of this: Imagine a chain of paperclips that are linked together. You can easily add another paperclip to the top or bottom. It’s also easy to insert one in the middle. All you have to do is to just break the chain at the middle, add a new paperclip, then reconnect the other half. A linked list is similar to this.

Example:

package MyPackage;
import java.util.LinkedList;
import java.util.ListIterator;
public class linkedlist {
public static void main(String args[]) {
/* Linked List Declaration */
LinkedList<String>l_list = new LinkedList<String>();
/*add(String Item) is used for adding
* the Items to the linked list*/
l_list.add("Java");
l_list.add("Python");
l_list.add("Scala");
l_list.add("Swift");
System.out.println("Linked List Content: " +l_list);
/*Add Items at specified position*/
l_list.add(2, "JavaScript");
l_list.add(3, "Kotlin");
System.out.println("l_list Content after editing: " +l_list);
/*Add First and Last Item*/
l_list.addFirst("First Course");
l_list.addLast("Last Course");
System.out.println("l_list Content after addition: " +l_list);
/*Get and set Items in the list*/
Object firstvar = l_list.get(0);
System.out.println("First Item: " +firstvar);
l_list.set(0, "Java9");
System.out.println("l_list Content after updating first Item: " +l_list);
/* Remove from a position*/
l_list.remove(1);
l_list.remove(2);
System.out.println("LinkedList after deletion of Item in 2nd and 3rd position " +l_list);
/*Remove first and last Item*/
l_list.removeFirst();
l_list.removeLast();
System.out.println("Final Content after removing first and last Item: "+l_list);
/*Iterating the linked list*/
ListIterator<String>itrator = l_list.listIterator();
System.out.println("List displayed using iterator:");
while (itrator.hasNext())
{
System.out.println(itrator.next());
}
}
}

Output:

Linked List Content = { Java, Python, Scala, Swift}
Content after editing ={ Java, Python, JavaScript, Kotlin, Scala, Swift }
Content after addition = { First Course, Java, Python, JavaScript, Kotlin, Scala, Swift, Last Course }
First Item = { First Course }
Content after updating first item = { Java9, Java, Python, JavaScript, Kotlin, Scala, Swift, Last Course }
Content after deletion of item in 2nd and 3rd position = { Java9, Python, Kotlin, Scala, Swift, Last Course }
Final Content after removing first and last Item = { Python, Kotlin, Scala, Swift }
List displayed using iterator =
Python
Kotlin
Scala
Swift

Now, let’s move ahead to the next topic.

What is an ArrayList?

ArrayList in Java is the implementation of List Interface where the elements can be dynamically added or removed from the corresponding list. Here, the size of the list is increased dynamically if the elements are added more than the initial or actual size. Though it may be slower than standard arrays, it can be helpful in programs where lots of manipulation in the array is required.

ArrayList is used for these purposes:

Let us move ahead and point out the similarities between LinkedList and ArrayList in Java.

Similarities between LinkedList and ArrayList

These are the notable similarities between LinkedList and ArrayList in Java.

Differences between LinkedList and ArrayList

First, let’s take a look at the parameters to compare LinkedList vs ArrayList in Java.

Parameters to compare LinkedList and ArrayList in Java:

  1. Operations

The insertion, addition and removal operations of an item are faster in a LinkedList because we don’t need to resize as we do in ArrayList. 

        2. Implementation

ArrayList is based on the concept of a dynamically resizable array, while LinkedList is based on doubly linked list implementation

3. Process

A LinkedList class can be used as a list and a queue because it implements List and Deque interfaces whereas ArrayList can only implement Lists.

4. Memory

LinkedList consumes more memory than an ArrayList because every node in a LinkedListstores two references, whereas ArrayList holds only data and its index

LinkedList vs ArrayList in Java

ParametersLinkedListArrayList
Operations

Insertion, addition, and removal operations are quite faster

Comparatively the operations are slow here

Implementation

Follows Doubly linked list implementation

Follows the concept of dynamically resizable array

Process

A LinkedList class can be a list and a queue because it implements List and Deque interfaces

An ArrayList class can be a list because it implements only Lists

Memory

Memory consumption in LinkedList is high

Less compared to LinkedList

That’s all folks! This brings us to the end of this article on the LinkedList vs ArrayList in Java. I hope you guys are clear with what is taught in this article.

If you found this article on “LinkedList vs ArrayList in Java”, check out the Java Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, and we come up with a curriculum which is designed for students and professionals who want to be a Java Developer.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 11th May,2024

11th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 1st June,2024

1st June

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR UiPath Selectors Tutorial