LinkedList Class in Java Tutorial with Example

Java LinkedList implements the List interface. It has the following properties:

1. LinkedList is a Doubly-linked list implementation of the List and Deque interfaces.
2. LinkedList can contain duplicate elements.
3. LinkedList class maintains insertion order.
4. LinkedList class manipulation is fast because no shifting needs to be occurred.
5. Java LinkedList class is unsynchronized.
6. Java LinkedList class can act as a stack, queue or list.

LinkedList Example



import java.util.*;

public class LinkedListExample {

   public static void main(String args[]) {

      // Declare LinkedList 
      LinkedList<String> ll=new LinkedList<String>();  

      // Adding elements to the LinkedList
        ll.add("A");
        ll.add("B");
        ll.addLast("C");
        ll.addFirst("D");
        ll.add(2, "E");
        ll.add("F");
        ll.add("G");
        System.out.println("LinkedList : " + ll);
 
        // Removing elements from the LinkedList
        ll.remove("C");
        ll.remove(2);
        ll.removeFirst();
        ll.removeLast();
        System.out.println("LinkedList after deletion: " + ll);
 
        // Finding elements in the LinkedList
        boolean status = ll.contains("A");
 
        if(status)
            System.out.println("List contains the element 'A' ");
        else
            System.out.println("List doesn't contain the element 'A'");
 
        // Number of elements in the LinkedList
        int size = ll.size();
        System.out.println("Size of LinkedList = " + size);
 
        // Get and set elements from LinkedList
        Object element = ll.get(2);
        System.out.println("Element returned by get() : " + element);
        ll.set(1, "Z");
        System.out.println("LinkedList after change : " + ll);
    }
}

Output :
LinkedList : [D, A, E, B, C, F, G]
LinkedList after deletion: [A, B, F]
List contains the element 'A' 
Size of LinkedList = 3
Element returned by get() : F
LinkedList after change : [A, Z, F]


LinkedList Methods

1. public boolean add(E e) : it adds the specified element to the end of the list.
2. public void addFirst(E e) : it adds the specified element at the beginning of the list.
3. public void addLast(E e) : it adds the specified element at the end of the list.
4. public void clear() : it removes all of the elements from the list.
5. public Object clone() : it returns the shallow copy of the LinkedList.
6. public boolean contains(Object o) : it returns true if the list contains the specified element.
7. public E get(int index) : it returns the element in the specified position in the list.
8. public E remove() : it retrieves and remove the head of the list.
9. public E remove(Object o) : it removes the first occurrence of the specified element from the list if it is present.
10. public E removeFirst() : it removes and returns the first element of the list.
11. public E removeLast() : it removes and returns the last element of the list.
12. public int size() : it returns the number of elements in the list.


LinkedList Tutorials


  • ArrayList vs LinkedList
  • Convert LinkedList to ArrayList


  • References : LinkedList Oracle Docs

    About The Author

    Subham Mittal has worked in Oracle for 3 years.
    Enjoyed this post? Never miss out on future posts by subscribing JavaHungry