Java LinkedList

In this tutorial, we will learn about what is java.util.LinkedList and methods of it with explanation, uses cases of it along with working examples of it.

LinkedList class

LinkedList is a class in Java that implements the List interface and backed by linked-list data structure for storing elements. Unlike ArrayList, which uses an array-based implementation, whereas LinkedList uses a doubly linked-list implementation that allows for efficient insertion and deletion of elements at any position in the list.

LinkedList particularly useful where list of elements needs to be added whose size is not known, where frequent insertions or deletions are required.

LinkedList class methods

The following are the methods of the LinkedList class, we will go through each method of it along with description of each method.

MethodDescription
add(E element)Adds the specified element to the end of the list.
addFirst(E element)Adds the specified element to the start of the list.
addLast(E elemet)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.
clear()Removes all the elements from the list.
contains(Object o)Returns true if the list contains the specified element.
get(int index)Returns the element at the specified position in the list.
indexOf(Object o)Returns the index of the first occurrence of the specified element in the list, or -1 if the list does not contain the element.
isEmpty()Returns true if the list contains no elements.
remove(Object o)Removes the first occurrence of the specified element from the list, if it is present.
remove(int index)Removes the element at the specified position in the list.
size()Returns the number of elements in the list.

LinkedList add(),remove(),addFirst(),addLast(),removeFirst() and removeLast() methods example:

The following is an example of LinkedList and will cover most of its method in this example.

CopiedCopy Code
import java.util.LinkedList;
public class LinkedListExample {
    public static void main(String[] args) {
        // Creating a LinkedList of Strings
        LinkedList<String> linkedList = new LinkedList<>();
        // Adding elements to the LinkedList
        linkedList.add("Tomato");
        linkedList.add("Cabbage");
        linkedList.add("Carrot");
        linkedList.add("Onion");
        // Adding an element at the beginning of the LinkedList
        linkedList.addFirst("Potato");
        // Adding an element at the end of the LinkedList
        linkedList.addLast("Capsicum");
        // Adding an element at the beginning of the LinkedList
        linkedList.addFirst("Potato");
        // Adding an element to the LinkedList
        linkedList.add("Beetroot");
        // Adding an element at a specific position in the LinkedList
        linkedList.add(1,"Broccoli");
        // Removing an element from the LinkedList
        linkedList.remove("Cabbage");
        // Removing the first and last elements from the LinkedList
        linkedList.removeFirst();
        linkedList.removeLast();
        // Printing the elements of the LinkedList
        System.out.println("Vegetables in the LinkedList: " + linkedList);
        // Retrieving the first and last elements from the LinkedList
        System.out.println("First element: " + linkedList.getFirst());
        System.out.println("Last element: " + linkedList.getLast());
        // Checking if the LinkedList contains a specific element
        System.out.println("LinkedList contains 'Carrot': " + linkedList.contains("Carrot"));
        // Checking the size of the LinkedList
        System.out.println("Size of the LinkedList: " + linkedList.size());
        // Clearing all the elements from the LinkedList
        linkedList.clear();
        // Checking if the LinkedList is empty
        System.out.println("Vegetables present in the list is empty? : " + linkedList.isEmpty());
    }
}

Output:

Vegetables in the LinkedList: [Broccoli, Potato, Tomato, Carrot, Onion, Capsicum]
First element: Broccoli
Last element: Capsicum
LinkedList contains 'Carrot': true
Size of the LinkedList: 6
Vegetables present in the list is empty? : true

Explanation:

In the above example, we have created a LinkedList for Vegetables of type String and performed operations of add, addFirst, addLast etc.

LinkedList provides a flexible and efficient way to store and manipulate data in a linked-list format, and can be particularly useful in scenarios where frequent additions and removals are expected.

This may not be suitable for situations where random access to elements is required, due to its O(n) access time complexity.

Conclusion:

In this tutorial, we have covered LinkedList class, its methods with explanation and a working example of LinkedList.