The LinkedList class in Java is a part of the Java Collections Framework and implements the List and Deque interfaces. It is a doubly-linked list that provides a dynamic data structure to store elements. Unlike arrays, LinkedList allows for efficient insertion and removal of elements at both ends and in the middle of the list.
Usage
LinkedList is used when frequent insertions and deletions are required. It is particularly useful when the size of the list is not known in advance or when elements need to be added or removed at both ends of the list.
importjava.util.LinkedList;publicclassLinkedListExample{publicstaticvoidmain(String[] args){LinkedList<String> list =newLinkedList<>();// Adding elements
list.add("Apple");
list.add("Banana");
list.add("Cherry");// Accessing elementsSystem.out.println("First element: "+ list.getFirst());System.out.println("Last element: "+ list.getLast());// Removing elements
list.removeFirst();
list.removeLast();System.out.println("List after removals: "+ list);}}Mogelijk gemaakt door
This example demonstrates basic operations such as adding, accessing, and removing elements from a LinkedList. We add three fruits to the list, access the first and last elements, and then remove them.
Example 2: Iterating Through a LinkedList
importjava.util.LinkedList;importjava.util.Iterator;publicclassLinkedListIteration{publicstaticvoidmain(String[] args){LinkedList<Integer> numbers =newLinkedList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);Iterator<Integer> iterator = numbers.iterator();while(iterator.hasNext()){System.out.println(iterator.next());}}}Mogelijk gemaakt door
In this example, we use an Iterator to traverse the LinkedList of integers. The Iterator provides a way to access each element sequentially.
Example 3: Using LinkedList as a Queue
importjava.util.LinkedList;importjava.util.Queue;publicclassLinkedListAsQueue{publicstaticvoidmain(String[] args){Queue<String> queue =newLinkedList<>();// Enqueue elements
queue.add("First");
queue.add("Second");
queue.add("Third");// Dequeue elementsSystem.out.println("Removed: "+ queue.poll());System.out.println("Queue after removal: "+ queue);}}Mogelijk gemaakt door
Here, LinkedList is used as a Queue. Elements are added using add() and removed using poll(), demonstrating FIFO (First-In-First-Out) behavior.
Tips and Best Practices
When to Use: Use LinkedList when you require frequent insertions and deletions, especially at the beginning or end of the list.
Performance Consideration: LinkedList has a higher overhead compared to ArrayList for accessing elements by index due to its sequential access nature.
The
LinkedListclass in Java is a part of the Java Collections Framework and implements theListandDequeinterfaces. It is a doubly-linked list that provides a dynamic data structure to store elements. Unlike arrays,LinkedListallows for efficient insertion and removal of elements at both ends and in the middle of the list.Usage
LinkedListis used when frequent insertions and deletions are required. It is particularly useful when the size of the list is not known in advance or when elements need to be added or removed at both ends of the list.Syntax
Type: The type of elements stored in the list.linkedList: The name of theLinkedListinstance.Examples
Example 1: Basic Operations
This example demonstrates basic operations such as adding, accessing, and removing elements from a
LinkedList. We add three fruits to the list, access the first and last elements, and then remove them.Example 2: Iterating Through a LinkedList
In this example, we use an
Iteratorto traverse theLinkedListof integers. TheIteratorprovides a way to access each element sequentially.Example 3: Using LinkedList as a Queue
Here,
LinkedListis used as aQueue. Elements are added usingadd()and removed usingpoll(), demonstrating FIFO (First-In-First-Out) behavior.Tips and Best Practices
LinkedListwhen you require frequent insertions and deletions, especially at the beginning or end of the list.LinkedListhas a higher overhead compared toArrayListfor accessing elements by index due to its sequential access nature.LinkedListallowsnullelements, so handle potentialNullPointerExceptionscenarios.LinkedListis not synchronized. UseCollections.synchronizedList(new LinkedList<>())for thread-safe operations.LinkedListas aDequefor double-ended queue operations, such asaddFirst(),addLast(),removeFirst(), andremoveLast().