Introduction
In Java, ArrayList and LinkedList are classes in java.util
package. Both of this data structure is used to store the ordered collection of an elements of same type. ArrayList is an resizeable array implementation of List interface. Whereas, LinkedList is doubly linked list implementation. Both of these implementation is not synchronized. So, If multiple threads are accessing an ArrayList or LinkedList instance concurrently, and if at least one of the threads modifies the list structurally. We need to externally synchronized the structure.
ArrayList vs LinkedList Comparison
Here, the table below lists some of the key differences between the ArrayList vs LinkedList class.
ArrayList | LinkedList |
---|---|
This class implements a List Interface. So, this acts as a list. | This class implements both List and Deque Interface. So, this acts as a both list and deque. |
It uses dynamic array to store the elements. | It uses the doubly linked list to store the elements. |
Remove operations with ArrayList is slow as it makes use of an array internally. So, if any element is removed from the array, all the bits are shifted in memory. | Remove operations with LinkedList is faster than the ArrayList. As no shifting is required on removal of an element. |
It only acts as a list. | It acts as a list and the queue. |
It is used in an application that only needs storing and accessing the data. | It is used in the application that needs the manipulation on data. |
As arrays have fixed length, we need to declare an ArrayList with some initial capacity. | We can create an empty list initially and add the nodes as and when needed. |
As arrays are indexed by int values in Java, we cannot store more than 2 raised to 32 elements. | There is no limitations on number of elements that can be stored. |
There is no memory overhead in ArrayList. | The LinkedList node needs two pointers to store the address of next and previous node leading to memory overhead. |
Examples demonstrating ArrayList vs LinkedList
Example 1 : Operations on ArrayList
In this example, we are performing some operations like add elements, add another list, remove elements, update the value, checking for the presence of elements and getting the size of the ArrayList.
// Program to demonstrate ArrayList vs LinkedList
import java.util.*;
public class Main
{
public static void main(String[] args)
{
// Initializing
ArrayList l1=new ArrayList<>();
ArrayList l2=new ArrayList<>(Arrays.asList(30,40,50));
// Adding the elements to ArrayList 1
l1.add(10);
l1.add(20);
l1.add(null);
System.out.println("ArrayList l1 = "+l1);
// appending ArrayList 2 to ArrayList 1
l1.addAll(l2);
System.out.println("After appending the ArrayList l2. ArrayList l1 = "+l1);
// Checking for the ArrayList elements
System.out.println("Does ArrayList contains 45"+l1.contains(45));
System.out.println("Does ArrayList contains 40"+l1.contains(40));
// Removing the element with value
l1.remove(null);
System.out.println("After removal ArrayList l1 = "+l1);
// Removing the element with Index
l1.remove(2);
System.out.println("After removing value from index 2. ArrayList l1 = "+l1);
// Update the element at index 2
l1.set(2,45);
System.out.println("After updating the value at index 2. ArrayList l1 = "+l1);
// Size of ArrayList
System.out.println("Size of ArrayList l1 = "+l1.size());
}
}
Output
ArrayList l1 = [10, 20, null]
After appending the ArrayList l2. ArrayList l1 = [10, 20, null, 30, 40, 50]
Does ArrayList contains 45false
Does ArrayList contains 40true
After removal ArrayList l1 = [10, 20, 30, 40, 50]
After removing value from index 2. ArrayList l1 = [10, 20, 40, 50]
After updating the value at index 2. ArrayList l1 = [10, 20, 45, 50]
Size of ArrayList l1 = 4
Example 2 : Operations on Linked List
In this example, we are performing some operations like add elements, add another list, using push, pop methods, remove elements, update the value, checking for the presence of elements and getting the size of the linked list.
// Program to demonstrate ArrayList vs LinkedList
import java.util.*;
public class Main
{
public static void main(String[] args)
{
// Initializing
LinkedList l1=new LinkedList<>();
LinkedList l2=new LinkedList<>(Arrays.asList(30,40,50));
// Adding the elements to list 1
l1.add(10);
l1.add(20);
l1.add(null);
// Adding elements at beginning and end of list
l1.addFirst(5);
l1.addLast(25);
System.out.println("Linked list is "+l1);
// appending list 2 to list 1
l1.addAll(l2);
System.out.println("Now, Linked list is "+l1);
// Checking for the list elements
System.out.println("Does list contains 45 "+l1.contains(45));
System.out.println("Does list contains 40 "+l1.contains(40));
// Removing the element with value
l1.remove(null);
System.out.println("After removal Linked list is "+l1);
// Removing the element with Index
l1.remove(2);
System.out.println("After removing value from index 2. Linked list is "+l1);
// Performs push and pop operation considering linked list as a StackOverflowError
l1.push(80);
System.out.println("After pushing 80 Linked list is "+l1);
System.out.println("Element popped out is "+l1.pop());
// Update the element at index 2
l1.set(2,45);
System.out.println("After updating the value at index 2. Linked list is "+l1);
// Size of List
System.out.println("Size of Linked list is "+l1.size());
}
}
Output
Linked list is [5, 10, 20, null, 25]
Now, Linked list is [5, 10, 20, null, 25, 30, 40, 50]
Does list contains 45 false
Does list contains 40 true
After removal Linked list is [5, 10, 20, 25, 30, 40, 50]
After removing value from index 2. Linked list is [5, 10, 25, 30, 40, 50]
After pushing 80 Linked list is [80, 5, 10, 25, 30, 40, 50]
Element popped out is 80
After updating the value at index 2. Linked list is [5, 10, 45, 30, 40, 50]
Size of Linked list is 6
Example 3 : Comparing the execution time of ArrayList vs LinkedList
In this example, we are comparing the time taken to execute some add and remove operations in the ArrayList vs LinkedList. Although, the time varies everytime we execute the code. But, the time taken by ArrayList will always be less than that of LinkedList operations.
// Program to demonstrate ArrayList vs LinkedList
import java.util.*;
import java.util.concurrent.TimeUnit;
public class Main
{
public static void main(String[] args)
{
// Initializing
long start = System.nanoTime();
ArrayList l1=new ArrayList<>();
ArrayList l2=new ArrayList<>(Arrays.asList(30,40,50));
// Adding the elements to ArrayList 1
l1.add(10);
l1.add(20);
l1.add(null);
// Removing the element with value
l1.remove(null);
l1.addAll(l2);
// Removing the element with Index
l1.remove(2);
// Update the element at index 2
l1.set(2,45);
long end = System.nanoTime();
long totaltime = end-start;
System.out.println("Time required for ArrayList "+totaltime+" nano seconds");
start = System.nanoTime();
LinkedList ll1=new LinkedList<>();
LinkedList ll2=new LinkedList<>(Arrays.asList(30,40,50));
// Adding the elements to LinkedList 1
ll1.add(10);
ll1.add(20);
ll1.add(null);
// Removing the element with value
ll1.remove(null);
ll1.addAll(ll2);
// Removing the element with Index
ll1.remove(2);
// Update the element at index 2
ll1.set(2,45);
end = System.nanoTime();
totaltime = end-start;
System.out.println("Time required for LinkedList "+totaltime+" nano seconds");
}
}
Output
Time required for ArrayList 157732 nano seconds
Time required for LinkedList 1332174 nano seconds
Summary
The knowledge of ArrayList vs LinkedList is very useful while working on real time applications in Java. In many situations, we will need to maintain the ordered collection of elements where we need to make a selection between ArrayList vs LinkedList class. So, depending upon the operations required and size of data we can make an appropriate selection among this two. In this tutorial, we covered some important methods used with a ArrayList vs LinkedList along with the example. As per the requirement of an application, we can choose an appropriate data structure. We learned in detail about this with an example.
All in all, this tutorial, covers everything that you need to know in order to have a clear view on ArrayList vs LinkedList in Java.
References
ArrayList Class
LinkedList Class
Further Reading
LinkedList in Java Explained [Complete Tutorial]