ArrayList and LinkedList are two commonly used classes in the Java Collections Framework. Although both classes implement the List interface and allow for the addition, removal, and retrieval of elements in a collection, there are some differences between the two in terms of performance and usage.
Performance:
ArrayList and LinkedList have different performance characteristics depending on the type of operation being performed.
Insertion and Deletion: LinkedList is faster than ArrayList for inserting and deleting elements in the middle of the list. This is because LinkedList implements a doubly linked list data structure, which allows for faster insertion and deletion of elements. ArrayList has to shift all the subsequent elements after an insertion or deletion operation, which can be costly for large lists. However, for inserting or deleting elements at the beginning or end of the list, ArrayList is faster because it doesn’t need to shift any elements.
Retrieval: ArrayList is faster than LinkedList for retrieving elements from the middle of the list. This is because ArrayList stores elements in a contiguous block of memory, so it can access elements in constant time using an index. LinkedList has to traverse the list to find the element, which can be slow for large lists. For retrieving elements at the beginning or end of the list, both ArrayList and LinkedList are equally fast.
Usage:
ArrayList and LinkedList are suited for different types of use cases.
ArrayList: ArrayList is suitable for use cases where the primary operation is random access retrieval, and the size of the list is fixed or changes infrequently. For example, if you want to store a large number of elements that you’ll access randomly, then ArrayList is the best choice.
LinkedList: LinkedList is suitable for use cases where the primary operation is insertion or deletion in the middle of the list, and the size of the list changes frequently. For example, if you want to implement a queue or a stack, then LinkedList is the best choice.
Example:
Here is an example code snippet that demonstrates the differences in performance between ArrayList and LinkedList for different types of operations:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;
public class ListPerformance {
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<>();
LinkedList<Integer> linkedList = new LinkedList<>();
// Insertion at the end
long startTime = System.nanoTime();
for (int i = 0; i < 100000; i++) {
arrayList.add(i);
}
long endTime = System.nanoTime();
System.out.println("ArrayList insertion time: " + (endTime - startTime) + " ns");
startTime = System.nanoTime();
for (int i = 0; i < 100000; i++) {
linkedList.add(i);
}
endTime = System.nanoTime();
System.out.println("LinkedList insertion time: " + (endTime - startTime) + " ns");
// Random access
Random random = new Random();
startTime = System.nanoTime();
for (int i = 0; i < 100000; i++) {
int index = random.nextInt(arrayList.size());
int element = arrayList.get(index);
}
endTime = System.nanoTime();
System.out.println("ArrayList random access time: " + (endTime - startTime) + " ns");
startTime = System.nanoTime();
for (int i = 0; i < 100000; i++) {
int index = random.nextInt(linkedList.size());
int element = linkedList.get(index);
}
endTime = System.nanoTime
}
}