ArrayList vs. LinkedList in Java: Differences Explained with Example
ArrayList and LinkedList both implements the Java List interface. However, which one you should use depends on the requirement. ArrayList and LinkedList classes are non-synchronized and both of them maintain insertion order which means the elements can be accessed in the same order, they are inserted into the list.
The following table will help you understand the differences between ArrayList and LinkedList in Java:
ArrayList | LinkedkList |
ArrayList internally uses resizable array for storing data items. | LinkedList internally uses doubly LinkedList for storing data items. |
ArrayList performs better when you only want to store and access data. | LinkedList performs better when you want to manipulate data. |
Manipulation of elements in ArrayList is slow because ArrayList internally uses dynamic array which means if an item is removed, all the bits are needed to shift in memory. | Manipulation of elements in LinkedList is faster than in ArrayList because LinkedList internally uses doubly LinkedList which means no bit shifting is needed for insertion and removal of elements. |
ArrayList implements only List interface. Therefore, it can only be used as a list. | LinkedList implements List and Deque interfaces. Therefore, LinkedList can be used as a list and queue. |
Insertion and removal of elements is slow in ArrayList as compared to LinkedList. The insertion and deletion performance time for ArrayList is O(n). | Insertion and removal of elements is faster in LinkedList as compared to ArrayList. The insertion and deletion performance time for LinkedList is O(1). |
ArrayList has less memory overhead as compared to LinkedList because each index in ArrayList only hold data. | The memory overhead in LinkedList is higher because each node in LinkedList hold both data and address of the previous and the next node. |
Search operation in ArrayList is faster as compared to LinkedList because elements can be accessed by index. The search performance for ArrayList is O(1). | Search operation in LinkedList is slow as compared to ArrayList because random element access is not allowed. The search performance for LinkedList is Big O of n or O(n). |
ArrayList Example
ArrayList should be used when the requirement is only to insert and access data because ArrayList performs better when there are frequent insertion and search operations.
Here's example of ArrayList in which the elements are inserted and accessed by its index:
import java.util.ArrayList; import java.util.List; public class Example { public static void main(String[] args) { // Creating a new ArrayList of String type ListanimalList = new ArrayList<>(); // Inserting elements animalList.add("Zebra"); animalList.add("Lion"); animalList.add("Monkey"); animalList.add("Tiger"); animalList.add("Crocodile"); animalList.add("Ape"); // Accessing elements by index for (int index = 0; index < animalList.size(); index++) { System.out.println("Element at index " + index + " is " + animalList.get(index)); } } }
The output of the above code is as follows:
Element at index 0 is Zebra Element at index 1 is Lion Element at index 2 is Monkey Element at index 3 is Tiger Element at index 4 is Crocodile Element at index 5 is Ape
LinkedList Example
LinkedList should be used when the requirement is to manipulate data because LinkedList performs better when there are frequent insertion and deletion operations.
Here's an example of LinkedList in which the elements are inserted and deleted:
import java.util.LinkedList; import java.util.List; public class Example { public static void main(String[] args) { // Creating a new LinkedList of String type List<String> animalList = new LinkedList<>(); // Inserting elements animalList.add("Zebra"); animalList.add("Lion"); animalList.add("Monkey"); animalList.add("Tiger"); animalList.add("Crocodile"); animalList.add("Ape"); // Printing before deleting elements System.out.println("List before deleting elements = " + animalList); // Deleting elements at index 1 and 4 animalList.remove(1); animalList.remove(4); // Printing after deleting elements System.out.println("List after deleting elements = " + animalList); } }
The output of the above code is as follows:
List before deleting elements = [Zebra, Lion, Monkey, Tiger, Crocodile, Ape] List after deleting elements = [Zebra, Monkey, Tiger, Crocodile]