LinkedList in Java Collections Framework
Table of Contents
- Introduction
- Understanding the LinkedList in Java
- Key LinkedList Operations in Java
- Working Example: Java LinkedList Operations
- Differences Between LinkedList and ArrayList
- Conclusion
Introduction
In this article, we will explore the LinkedList class in Java, which is part of the Java Collections Framework. LinkedList is an important data structure that allows for efficient insertion and deletion of elements. We will dive into the key concepts, basic operations, and provide a step-by-step guide to using LinkedList in Java, particularly focusing on a simple example extracted from the project files.
Understanding the LinkedList in Java
The LinkedList
class implements both the List
and Deque
interfaces, making it a versatile data structure that allows insertion and removal from both ends. In contrast to ArrayList
, LinkedList uses a doubly linked list to store elements. Each element in the list is represented as a node, which consists of data and references (or pointers) to the next and previous nodes.
Key Properties of LinkedList:
- Implements both
List
andDeque
interfaces. - Allows null elements.
- Supports insertion at the beginning, middle, and end of the list with O(1) complexity.
When to Use LinkedList:
- Frequent Insertions and Deletions: If your use case involves frequent insertions and deletions, LinkedList is ideal because these operations have O(1) complexity compared to
ArrayList
‘s O(n). - Bidirectional Traversal: If you need to traverse the list in both directions, LinkedList is efficient because each node has pointers to both the previous and the next node.
Key LinkedList Operations in Java
The most commonly used operations for LinkedList include:
add(E element)
: Adds the specified element to the end of the list.add(int index, E element)
: Inserts the specified element at the specified position in the list.remove(E element)
: Removes the first occurrence of the specified element from the list.get(int index)
: Returns the element at the specified position in the list.clear()
: Removes all elements from the list.
Code Example: LinkedList Operations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.LinkedList; public class Main { public static void main(String[] args) { LinkedList<String> list = new LinkedList<>(); // Adding elements to the LinkedList list.add("Chaand"); list.add("Jai"); list.add("Veeru"); // Adding element at the first position list.addFirst("StudyEasy"); // Removing the first occurrence of an element list.remove("Jai"); // Printing the elements of LinkedList for (String name : list) { System.out.println(name); } } } |
Step-by-Step Explanation:
- Adding Elements: Elements are added to the list using the
add()
method. In the above code, “Chaand,” “Jai,” and “Veeru” are added. addFirst()
: This method inserts an element at the beginning of the list. In this case, “StudyEasy” is inserted at the start.- Removing Elements: The
remove()
method removes the first occurrence of the specified element, in this case, “Jai.” - Iteration: The
for-each
loop is used to iterate over the list and print all the elements.
Output:
1 2 3 |
StudyEasy Chaand Veeru |
Differences Between LinkedList and ArrayList
Feature | LinkedList | ArrayList |
---|---|---|
Underlying Data Structure | Doubly Linked List | Resizable Array |
Insertion/Deletion | O(1) for inserting/deleting at the ends | O(n) for inserting/deleting at any position |
Access Time | O(n) (sequential access) | O(1) for random access (indexed access) |
Memory Usage | Higher due to node pointers | Lower, contiguous memory allocation |
Conclusion
The LinkedList class in Java is a powerful data structure when frequent insertions and deletions are necessary. By understanding its core operations and differences from ArrayList, developers can make informed decisions about when to use LinkedList in their projects.