LinkedList in Java with Example

LinkedList is a doubly-linked list implementation of the List and Deque interfaces. LinkedList allow insertion and deletion in middle more faster than arraylist. LinkedList allow support both forward and backward direction traversal but does not support random access.

Features of LinkedList

  • It can contain duplicate elements.
  • It maintains insertion order.
  • It is non synchronized means cannot be used in multi-threaded environment.
  • Insertion and Deletion are fast because no shifting needs to be occurred.
  • It can be used as list, stack or queue.

Difference between ArrayList and LinkedList

ArrayListLinkedList
1. It internally uses dynamic array to store the elements.1. It internally uses doubly linked list to store the elements.
2. Insertion and Deletion in ArrayList is slow as it lots of shifting operation2. Faster than ArrayList as it uses doubly linked list so no bit shifting is required in memory.
3. Better for fast traversal3. Best for Insertion and Deletion

Constructor of LinkedList class

  1. LinkedList()
  2. LinkedList(Collection c)

Method of LinkedList class

  1. boolean add(Object item): It adds the element at the end of the list and returns true if successfully added.
    
    LinkedList<String> list = new LinkedList<String>();
     list.add("Item 1");
     list.add("Item 2");
    
  2. void add(int index, Object item): It adds an element at the given index of the the list.
     LinkedList<String> list = new LinkedList<String>();
     list.add("Item 1");
     list.add("Item 3");
     list.add(1, "Item 2"); // It will add item 2 to the 2nd index
    
  3. void addAll(Collection c): It adds all the elements of the specified collection c to the linked list. It throws NullPointerException if the specified collection is null.
     LinkedList<String> list1 = new LinkedList<String>();</pre>
    
     ArrayList<String> list2 = new ArrayList<String>();
     list2.add("Item 1");
     list2.add("Item 2");
     list2.add("Item 3");
     list1.add(list2);
    
  4. void addFirst(Object item): It adds the item (or element) at the first position in the list.
     LinkedList<String> list = new LinkedList<String>();
     list.add("Item 2");
     list.addFirst("Item 1"); //Item 1 will be placed at first index
    
  5. void addLast(Object item): It inserts the specified item at the end of the list.
     
    LinkedList<String> list = new LinkedList<String>(); 
    list.add("Item 1");
    list.add("Item 2"); 
    list.addLast("Item 3"); 
    
  6. boolean contains(Object item) : It checks whether the given item is present in the list or not. If the item is present then it returns true else false.
    
     LinkedList<String> list = new LinkedList<String>();
     list.add("Item 1");
     list.add("Item 2");
     if(list.contains("Item 1"))
     System.out.println("Element Found");
    
  7. void clear(): It removes all the elements from the list.
    LinkedList<String> list = new LinkedList<String>();
    list.add("Item 1");
    list.add("Item 2");
    list.clear();
    
  8. Object get(int index): It returns the item of the specified index from the list.
     
    LinkedList<String> list = new LinkedList<String>(); 
    list.add("Item 1"); 
    list.add("Item 2"); 
    String item = list.get(0); // first element will be returned
     
  9. Object remove(): It removes the first element of the list.
     
    LinkedList<String> list = new LinkedList<String>(); 
    list.add("Item 1"); 
    list.add("Item 2"); 
    list.remove() // remove the first element from the list
    
  10. Object set(int index, Object item): It updates the item of specified index with the give value.
     
    LinkedList<String> list = new LinkedList<String>(); 
    list.add("Item 1"); 
    list.add("Item 2"); 
    list.set(0,"First Item") // update item 1 to First Item
    
  11. Object remove(int index): It removes the element from the list that is present at the specified index.
     
    LinkedList<String> list = new LinkedList<String>(); 
    list.add("Item 1"); 
    list.add("Item 2"); 
    list.remove(1) // remove the 2nd element from the list
    
  12. Object removeFirst(): It removes the first element from the list.
     
    LinkedList<String> list = new LinkedList<String>(); 
    list.add("Item 1"); 
    list.add("Item 2"); 
    list.removeFirst() // remove the 1st element from the list
    
  13. Object removeLast(): It removes the last element from the list.
    LinkedList<String> list = new LinkedList<String>(); 
    list.add("Item 1"); 
    list.add("Item 2"); 
    list.removeLast() // remove the last element from the list
    

Complete Example of LinkedList


package com.startwithjava;

import java.util.LinkedList;

public class LinkedListMain {
 public static void main(String[] args) {
    LinkedList<String> list = new LinkedList<String>();
    /*Adding elements*/
     list.add("Item 1");
     list.add("Item 2");
     list.add("Item 3");

    /*Displaying Linked List elements*/
     System.out.println("Linked List Items: " +list);

    /*Add First and Last Elements*/
    list.addFirst("First Item"); 
    list.addLast("Last Item");
    System.out.println("LinkedList Contents after addition: " +list);
    /*Getting and setting elements*/
    Object first_item = list.get(0);
    System.out.println("First element: " +first_item);
    list.set(0, "Update item 1");
    Object first_item2= list.get(0);
    System.out.println("First element after update by set method: " +first_item2);

   /*Remove first and last element*/
   list.removeFirst();
   list.removeLast();
   System.out.println("LinkedList after deletion of first and last element: " +list);

   /*Adding and removing element with the specified position*/
	list.add(0, "Item New");
	list.remove(2);
	System.out.println("Final Content: " +list); 
   }

}

Leave a Reply