Difference Between ArrayList and LinkedList in Java with Examples
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). |
When to Use ArrayList in Java?
Use ArrayList if the requirement is only to insert and access data because ArrayList performs better when there are frequent insertion and search operations.
When to Use LinkedList in Java?
Use LinkedList if the requirement is to manipulate data because LinkedList performs better when there are frequent insertion and deletion operations.
ArrayList Example
Here is an example of ArrayList in which the elements are inserted and accessed by index:
import java.util.ArrayList;
import java.util.List;
public class ArrayListExample {
public static void main(String[] args) {
List<String> animalList = new ArrayList<>();
// inserting elements
animalList.add("Zebra");
animalList.add("Lion");
animalList.add("Monkey");
animalList.add("Tiger");
animalList.add("Crocodile");
animalList.add("Ape");
// accessing elements
for (int index = 0; index < animalList.size(); index++) {
System.out.println("Element at index " + index + " is " + animalList.get(index));
}
}
}
Output:
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
Here is an example of LinkedList in which the elements are inserted and deleted:
import java.util.LinkedList;
import java.util.List;
public class ListExample {
public static void main(String[] args) {
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");
System.out.println("List before deleting elements = " + animalList);
// deleting elements
animalList.remove(1);
animalList.remove(4);
System.out.println("List after deleting elements = " + animalList);
}
}
Output:
List after deleting elements = [Zebra, Monkey, Tiger, Crocodile]