Java LinkedList

Posted by Hayri Cicek on September 18, 2019


Java LinkedList is a linear data structure, which means the elements are linked using pointers.
LinkedList can have duplicate and null values and maintains insertion order.
The LinkedList can be used as a List, Queue or Stack because the LinkedList class implements List and Deque interfaces.

The following example shows how to create a LinkedList and add elements to it.


import java.util.LinkedList;

public class LinkedListAddExample {
    public static void main(String[] args) {
        LinkedList<String> cars = new LinkedList<>();

        cars.add("BMW");
        cars.add("Volvo");
        cars.add("Toyota");
        cars.add("Opel");

        System.out.println("Cars: " + cars);

        // add element at specific position
        cars.add(2, "Nissan");
        System.out.println("Cars: " + cars);

        cars.addFirst("Subaru");
        cars.addLast("Peugeot");

        System.out.println("Cars : " + cars);

        // add new list of element to the first list
        LinkedList<String> cars2 = new LinkedList<>();
        cars2.add("Mercedes");
        cars2.add("Dodge");
        cars2.add("Lexus");
        cars2.add("Audi");

        // use addAll() method to add the second list to the first list
        cars.addAll(cars2);

        System.out.println("Cars after addAll() : " + cars);
    }
}


add() method will append specified element to the end of the list.
addFirst() method will insert the given element at the beginning of the list.
addLast() method will append specified element to the end of the list.
addAll() method will append all of the elements in the specified collection to the end of the list.
Output:

$ javac LinkedListAddExample.java
$ java LinkedListAddExample
Cars: [BMW, Volvo, Toyota, Opel]
Cars: [BMW, Volvo, Nissan, Toyota, Opel]
Cars : [Subaru, BMW, Volvo, Nissan, Toyota, Opel, Peugeot]
Cars after addAll() : [Subaru, BMW, Volvo, Nissan, Toyota, Opel, Peugeot, Mercedes, Dodge, Lexus, Audi]


The following example shows how to use a few of the LinkedList methods.
We will learn how to get elements, remove elements, check the size, check if it's empty

import java.util.LinkedList;

public class LinkedListMethodsExample {
    public static void main(String[] args) {
        LinkedList<String> cars = new LinkedList<>();

        cars.add("BMW");
        cars.add("Volvo");
        cars.add("Toyota");
        cars.add("Opel");
        cars.add("Candy");

        System.out.println("Is it empty: " + cars.isEmpty());
        System.out.println("Get element at given position " + cars.get(1));
        System.out.println("Get first element: " + cars.getFirst());
        System.out.println("Get last element: " + cars.getLast());
        System.out.println("Contains Candy: " + cars.contains("Candy"));
        System.out.println("Remove Candy: " + cars.remove("Candy"));
        System.out.println("Contains Candy: " + cars.contains("Candy"));
        System.out.println("Remove first: " + cars.removeFirst());
        System.out.println("Remove last: " + cars.removeLast());
        System.out.println("Size : " + cars.size());
        cars.clear();
        System.out.println("Is it empty: " + cars.isEmpty());
    }
}


isEmpty() method will return true if the list is empty.
get() method will return the element at the specified position.
getFirst() method will return the first element.
getLast() method will return the last element.
contains() method will return true if the list contains the specified element.
remove() method will remove the element from the list.
removeFirst() method will remove the first element from the list.
removeLast() method will remove the last element from the list.
size() method will return the number of elements in the list.
clear() method will remove all elements from the list.
Output:

$ javac LinkedListMethodsExample.java
$ java LinkedListMethodsExample
Is it empty: false
Get element at given position Volvo
Get first element: BMW
Get last element: Candy
Contains Candy: true
Remove Candy: true
Contains Candy: false
Remove first: BMW
Remove last: Opel
Size : 2
Is it empty: true


The following example shows how to iterate over a LinkedList.
We will learn using Java 8 forEach(), iterator() method, forEachRemaining() method and the for-each loop.

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

public class IterateLinkedListExample {
    public static void main(String[] args) {
        LinkedList<String> cars = new LinkedList<>();

        cars.add("BMW");
        cars.add("Volvo");
        cars.add("Toyota");
        cars.add("Opel");

        // Using the for-each loop
        System.out.println("for-each loop");
        for (String car : cars) {
            System.out.println(car);
        }
        
        // Java 8 forEach loop
        System.out.println("\nJava 8 forEach loop");
        cars.forEach(car -> {
            System.out.println(car);
        });

        // iterator() method
        System.out.println("\niterator() method");
        Iterator<String> carsItr = cars.iterator();
        while (carsItr.hasNext()) {
            String car = carsItr.next();
            System.out.println(car);
        }

        // forEachRemaining() method
        System.out.println("\nforEachRemaining() method");
        carsItr = cars.iterator();
        carsItr.forEachRemaining(car -> {
            System.out.println(car);
        });
    }
}


Output:

$ javac IterateLinkedListExample.java
$ java IterateLinkedListExample
for-each loop
BMW
Volvo
Toyota
Opel

Java 8 forEach loop
BMW
Volvo
Toyota
Opel

iterator() method
BMW
Volvo
Toyota
Opel

forEachRemaining() method
BMW
Volvo
Toyota
Opel


Share this: