Post

LinkedList in Java Collections



Introduction

In Java, LinkedList is a class that implements the List interface and extends the AbstractSequentialList class. It represents a doubly linked list, where each element is stored as a separate object called a node. Each node contains a reference to the next node in the list as well as a reference to the previous node. LinkedLists provide efficient insertion and deletion operations, making them suitable for scenarios where frequent modifications are required. In this article, we will explore the LinkedList class in Java, its characteristics, usage scenarios, and operations.

Characteristics of LinkedList

  1. Doubly Linked Structure
    Each element (node) in a LinkedList holds a reference to both the next and previous elements. This allows for efficient traversal in both directions.
  2. Dynamic Size
    Unlike arrays, LinkedLists do not have a fixed size. They can grow or shrink dynamically as elements are added or removed.
  3. Non-Synchronized
    LinkedLists are not synchronized, meaning they are not thread-safe by default. If thread safety is required, synchronization must be handled externally.
  4. Null Elements
    LinkedLists can contain null elements.

Creating a LinkedList

You can create a LinkedList in Java using the following syntax:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.LinkedList;

public class Main {
    public static void main(String[] args) {
        // Creating a LinkedList
        LinkedList<String> linkedList = new LinkedList<>();

        // Adding elements to the LinkedList
        linkedList.add("Java");
        linkedList.add("Python");
        linkedList.add("C++");

        // Displaying the LinkedList
        System.out.println("LinkedList: " + linkedList);
    }
}

Operations on LinkedList

Adding Elements

Elements can be added to a LinkedList using various methods:

  • add(E e): Appends the element to the end of the list.

    1
    
    linkedList.add("JavaScript");
    
  • add(int index, E element): Inserts the element at the specified position in the list.

    1
    
    linkedList.add(1, "Ruby");
    

Accessing Elements

You can access elements in a LinkedList using:

  • get(int index): Retrieves the element at the specified index.

    1
    
    String element = linkedList.get(2);
    

Removing Elements

Elements can be removed from a LinkedList using:

  • remove(): Removes and returns the first element from the list.

    1
    
    linkedList.remove();
    
  • remove(int index): Removes the element at the specified index from the list.

    1
    
    linkedList.remove(1);
    

Iterating Over Elements

You can iterate over elements in a LinkedList using an iterator or enhanced for-loop:

1
2
3
4
5
6
7
8
9
10
11
// Using iterator
Iterator<String> iterator = linkedList.iterator();
while (iterator.hasNext()) {
    String element = iterator.next();
    System.out.println(element);
}

// Using enhanced for-loop
for (String element : linkedList) {
    System.out.println(element);
}

Other Operations

  • size(): Returns the number of elements in the list.

    1
    
    int size = linkedList.size();
    
  • clear(): Removes all elements from the list.

    1
    
    linkedList.clear();
    

Use Cases for LinkedList

LinkedLists are suitable for scenarios where frequent insertion and deletion operations are required, such as:

  • Implementing queues and dequeues where elements are added or removed from both ends.
  • Implementing graphs and certain algorithms where efficient node manipulation is essential.
  • Use cases where the size of the data structure may vary dynamically.

Performance Considerations

  • Insertion and Deletion
    LinkedLists offer O(1) time complexity for insertion and deletion operations at both ends (head and tail). However, accessing elements by index has a time complexity of O(n) as it requires traversal from the beginning or end of the list.

  • Memory Overhead
    LinkedLists consume more memory than arrays due to the additional storage required for references to the next and previous nodes.

Conclusion

LinkedLists in Java provide a flexible and efficient way to manage collections of elements where frequent modifications are expected. They offer dynamic resizing, efficient insertion and deletion operations, and support bidirectional traversal through nodes. Understanding when and how to use LinkedLists can greatly enhance your ability to design and implement efficient data structures and algorithms in Java applications. By leveraging the LinkedList class and its operations effectively, you can build robust and scalable solutions that meet diverse programming challenges.

© 2024 Java Tutorial Online. All rights reserved.