Skip to main content
Documents
Share
LinkedIn
Facebook
Twitter
Copy
Java keywordsIntroduction To JavaJava File HandlingJava Language BasicsJava ArraysJava Object-Oriented Programming

Java LinkedList

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.

Syntax


LinkedList<Type> linkedList = new LinkedList<>();
  • Type: The type of elements stored in the list.
  • linkedList: The name of the LinkedList instance.

Examples

Example 1: Basic Operations


import java.util.LinkedList;

public class LinkedListExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        
        // Adding elements
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");
        
        // Accessing elements
        System.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);
    }
}

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


import java.util.LinkedList;
import java.util.Iterator;

public class LinkedListIteration {
    public static void main(String[] args) {
        LinkedList<Integer> numbers = new LinkedList<>();
        numbers.add(10);
        numbers.add(20);
        numbers.add(30);
        
        Iterator<Integer> iterator = numbers.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

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


import java.util.LinkedList;
import java.util.Queue;

public class LinkedListAsQueue {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<>();
        
        // Enqueue elements
        queue.add("First");
        queue.add("Second");
        queue.add("Third");
        
        // Dequeue elements
        System.out.println("Removed: " + queue.poll());
        System.out.println("Queue after removal: " + queue);
    }
}

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.
  • Null Elements: LinkedList allows null elements, so handle potential NullPointerException scenarios.
  • Thread Safety: LinkedList is not synchronized. Use Collections.synchronizedList(new LinkedList<>()) for thread-safe operations.
  • Deque Operations: Utilize LinkedList as a Deque for double-ended queue operations, such as addFirst(), addLast(), removeFirst(), and removeLast().