«Back to Home

Core Java

Topics

Difference Between ArrayList And LinkedList In Java

In Java collections, both ArrayList and LinkedList classes implements List interface and maintains an insertion order and both classes are non-synchronized.
 
Difference between ArrayList and LinkedList classes

There are many differences between ArrayList and LinkedList classes, which are,

 ArrayList  LinkedList
 ArrayList uses dynamic array to store the elements internally.  LinkedList uses doubly linked list to store the elements internally.
Its manipulation is slow because it internally uses an array. If any element is removed from the array, all the bits are shifted in the memory. Its manipulation is faster than ArrayList because it uses doubly linked list. Thus no bit shifting is required in memory.
ArrayList class can perform as a list only because it implements List only. LinkedList class can perform as a list and queue both because it implements List and Deque interfaces.
ArrayList is good for storing and accessing data. LinkedList is good for manipulating data.
ArrayList cannot be iterated in reverse direction. LinkedList can be iterated in reverse direction.
 
Let’s see an example of ArrayList and LinkedList.
 
Code
  1. import java.util.*;  
  2. public class CollectionsExample {  
  3.     public static void main(String args[]) {  
  4.         List < String > a = new ArrayList < String > ();  
  5.         a.add("Harry"); //add object in arraylist  
  6.         a.add("Marria");  
  7.         a.add("Mia");  
  8.         a.add("Mia");  
  9.         List < String > l = new LinkedList < String > ();  
  10.         l.add("Sophia"); //add object in linkedlist  
  11.         l.add("David");  
  12.         l.add("James");  
  13.         l.add("John");  
  14.         System.out.println("Arraylist class: " + a);  
  15.         System.out.println("Linkedlist class: " + l);  
  16.     }  
  17. }  
27

Output

28

Summary

Thus, we learnt that both ArrayList and LinkedList classes implements list interface, maintains an insertion order as both classes are non-synchronized and also learnt their differences in Java.