Java LinkedList Class

Category: Java   Tags: Java, Java Collection, Java List Interface, Java LinkedList Class

LinkedList class extends AbstractSequentialList and implements the List, Deque, and Queue interfaces. It provides a linked-list data structure. LinkedList is a generic class that has this declaration:

                                    public class LinkedList<E>
                                    extends AbstractSequentialList<E>
                                    implements List<E>, Deque<E>, Cloneable, Serializable
                                

Here, E specifies the type of object that this list will contain.

Constructors

public LinkedList()
Constructs an empty list.
public LinkedList(Collection<? extends E> c)
It creates a LinkedList containing the elements of the specified collection c.

LinkedList is not synchronized. Therefore if multiple thread is accessing the list then it must synchronized externally. You can check out this post about synchronization. You can use Collections.synchronizedList also to synchronize the LinkedList:

                                    List list = Collections.synchronizedList(new LinkedList());
                                

The returned iterator of this class are fail-fast i.e during iterating the linkedlist if you do any modification, it will throw ConcurrentModificationException exception. Here is one LinkedList example:

JavaLinkedListDemo.java

                            package com.tutorial.java.collections;

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

                            public class JavaLinkedListDemo {

                                public static void main(String[] args) {
                                    LinkedList<Integer> list = new LinkedList<>>();
                                    list.add(10);
                                    list.add(11);
                                    System.out.println("List: " + list.toString());

                                    //Add element to the first position
                                    list.addFirst(90);
                                    System.out.println("After addFirst(): " + list.toString());

                                    //Add element to the last position
                                    list.addLast(100);
                                    System.out.println("After addLast(): " + list.toString());

                                    System.out.println("Get first element: " + list.getFirst());
                                    System.out.println("Get last element: " + list.getLast());

                                    //Iterate the LinkedList
                                    System.out.println("LinkedList iteration: ");
                                    Iterator<Integer> iterator = list.iterator();
                                    while (iterator.hasNext()) {
                                        Integer element = iterator.next();
                                        System.out.println(element);
                                    }

                                    //Iterate the LinkedList in reverse order
                                    System.out.println("LinkedList iteration: ");
                                    Iterator<Integer> iterator2 = list.descendingIterator();
                                    while (iterator2.hasNext()) {
                                        Integer element = iterator2.next();
                                        System.out.println(element);
                                    }

                                    // Removes first element
                                    list.removeFirst();
                                    System.out.println("After removeFirst(): " + list.toString());

                                    // Removes last element
                                    list.removeLast();
                                    System.out.println("After removeLast(): " + list.toString());

                                    // Removes last element
                                    list.pop();
                                    System.out.println("After pop(): " + list.toString());
                                }

                            }
                        

Output:

                            List: [10, 11]
                            After addFirst(): [90, 10, 11]
                            After addLast(): [90, 10, 11, 100]
                            Get first element: 90
                            Get last element: 100
                            LinkedList iteration:
                            90
                            10
                            11
                            100
                            LinkedList iteration:
                            100
                            11
                            10
                            90
                            After removeFirst(): [10, 11, 100]
                            After removeLast(): [10, 11]
                            After pop(): [11]