Difference between ArrayList and LinkedList in java

Here we will see the difference between ArrayList and LinkedList in java with example. First, we will see all the points in brief later we will discuss in details.

ArrayListLinkedList
1. ArrayList internally uses dynamic array to store the elements.1. LinkedList internally uses doubly linked list to store the elements.
ArrayList extends AbstractList and implements RandomAccess interface.LinkedList extends AbstractSequentialList and implements Deque interface.
Accessing elements from the ArrayList is faster than LinkedList.Accessing elements from the LinkedList is slower than ArrayList.
Removing any elements takes more time in case of ArrayList than LinkedList.Remove operation in case of LinkedList is faster than ArrayList.
ArrayList is frequently used Collection class.We rarely use LinkedList in real time programing.

 

Let’s see all points in details.

Example of ArrayList and LinkedList –

 ArrayList example –

import java.util.ArrayList;
import java.util.List;
 
public class ArraListExample {
 
	public static void main(String[] args) {
 
		List<String> listObject = new ArrayList<>();
 
		listObject.add("ram");
		listObject.add("mohan");
		listObject.add("shyam");
		listObject.add("mohan");
		listObject.add("ram");
 
		System.out.println(listObject);
	}
}

Output is – [ram, mohan, shyam, mohan, ram]

Example of LinkedList –

import java.util.LinkedList;
import java.util.List;
 
public class LinkedListExample {
	public static void main(String[] args) {
		List<String> listObject = new LinkedList<>();
 
		listObject.add("ram");
		listObject.add("mohan");
		listObject.add("shyam");
		listObject.add("mohan");
		listObject.add("ram");
		listObject.add(null);
 
		System.out.println(listObject);
 
	}
}

Output is – [ram, mohan, shyam, mohan, ram, null]

 

ArrayList internally uses a dynamic array to store the elements whereas LinkedList internally uses a doubly linked list to store the elements.

ArrayList internally uses an array which is dynamic in nature i.e size of ArrayList increase automatically once it will reach the default capacity(10). For example, as soon as we add 10th element new capacity of ArrayList will be 15.

LinkedList internally uses DoublyLinkedList i.e Node based implementation.


 

ArrayList extends AbstractList and implements RandomAccess interface whereas LinkedList extends AbstractSequentialList and implements Deque interface.

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{

}
public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{

}

 

Accessing elements from the ArrayList is faster than LinkedList. In below example, we will see how accessing the elements from ArrayList is faster than LinkedList.


package rrrr;
import java.util.*;
public class ArrayListVsLinkedListGetRExamp {
public static void main(String[] args) {
	
	
	List<String> listOfArrayList = new ArrayList<>();
	
    for(int i=0; i<10000000; i++) {
		listOfArrayList.add("ram");
	}
    
	//get current time in milliseconds
	long l1 = System.currentTimeMillis();
	listOfArrayList.get(1000000);
	long l2 = System.currentTimeMillis();
	//l2 - l1 will return time took to add 1M times ram in listOfArrayList
	System.out.println("time taken to retrieve by ArrayList "+(l2-l1) +" "+"millisec");
	
	
	List<String> listOfLinkedList = new LinkedList<>();
	for(int i=0; i<10000000; i++) {
		listOfLinkedList.add("ram");
	}
	
	long l3 = System.currentTimeMillis();
	listOfLinkedList.get(1000000);
	long l4 = System.currentTimeMillis();
	
	//l4 - l3 will return time took to add 1M times ram in listOfVector
	System.out.println("time taken to retrieve by LinkedList "+(l4-l3) +" "+"millisec");
	
	
}
}

Output is –

time taken to retrieve by ArrayList 0 millisec
time taken to retrieve by LinkedList 6 millisec

 

In the above example, we added 10000000 elements in ArrayList and LinkedList and we retrieved 1000000 elements. Time is taken by ArrayList is 0 sec whereas time is taken by LinkedList is 6 sec. Time may vary but it will always more for LinkedList than ArrayList.

 

Removing any elements takes more time in case of ArrayList than LinkedList.

Here we will see how remove() takes more time for ArrayList than LinkedList.

package addingtime;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class RemoveElementExample {
public static void main(String[] args) {
List<String> listOfArrayList = new ArrayList<>();
	
    for(int i=0; i<10000000; i++) {
		listOfArrayList.add("ram");
	}
    
	//get current time in milliseconds
	long l1 = System.currentTimeMillis();
	listOfArrayList.remove(1000000);
	long l2 = System.currentTimeMillis();
	//l2 - l1 will return time took to add 1M times ram in listOfArrayList
	System.out.println("time taken to remove by ArrayList "+(l2-l1) +" "+"millisec");
	
	
	List<String> listOfLinkedList = new LinkedList<>();
	for(int i=0; i<10000000; i++) {
		listOfLinkedList.add("ram");
	}
	
	long l3 = System.currentTimeMillis();
	listOfLinkedList.remove(1000000);
	long l4 = System.currentTimeMillis();
	
	//l4 - l3 will return time took to add 1M times ram in listOfVector
	System.out.println("time taken to remove by LinkedList "+(l4-l3) +" "+"millisec");
	
}
}

 

Output is –

time taken to remove by ArrayList 11 millisec
time taken to remove by LinkedList 6 millisec

 

We can see when we try to remove the nth elements ArrayList is taking almost double time than LinkedList.

 

And finally Yes We generally use ArrayList in real time development.