Concrete Collections in Java - LinkedList

Collection Framework Hierarchy

In the previous blog, we learned about Collections in Java. If you want to learn about Collections, visit Collections in Java . In this blog, we will learn about Concrete Collections in Java. Following are the Classes in the Collection framework. We will discover them one by one. We will go through the Concrete Collection named LinkedList. LinkedList stores the object in a separate link. Each link stores a reference to the next link in the sequence.


Collection Framework Hierarchy

...

LinkedList

We have already learned about Arrays and ArrayList in our previous blogs. But Array and ArrayList have major drawbacks. The main drawback is, in an array and array list Removing an element from the middle of an array is costly because all array elements beyond the removed one must be moved toward the start of the array. The same is applicable for inserting the elements in the middle. In order to solve this problem Collection Framework has a data structure called LinkedList. LinkedList stores the object in a separate link. Each link stores a reference to the next link in the sequence. All the Linked Lists in Java are doubly Linked i.e. each link in the linked list stores the reference of its predecessor.

...

In the above figure, we can three fields i.e. data, next and previous. We will see all 3 fields one by one:

data - It stores the actual data.

next - It stores the address of the next element in the linked list. By default, the value of the first element is set as null.

previous - It stored the address of the previous element in the linked list. By default, the value of the last element is set as null.

...

In the above diagram, we can see that the previous field is null and the next field holds the address of the next node data field i.e. C. The next field of the C node holds the address of the data field i.e. C++. The previous field of the C++ node holds the address of the data field of the C node.


Syntax for Creating a LinkedList

Syntax

LinkedList<TypeOfLinkedList> linkedListObj = new LinkedList<>();

e.g.

LinkedList<String> linkedListObj = new LinkedList<>();

Example of Creating a LinkedList:

import java.util.LinkedList;
class LinkedListEx {
  public static void main(String[] args){
  // Creating String LinkedList
    LinkedList<String> languages = new LinkedList<>();
  // Add elements to LinkedList
    languages.add("Java");
    languages.add("C");
    languages.add("C++");
    languages.add("React");
    System.out.println("LinkedList1: " + languages);
  // Creating Integer LinkedList
    LinkedList<Integer> numbers = new LinkedList<>();
  // Add elements to LinkedList
    numbers.add(10);
    numbers.add(20);
    numbers.add(30);
    numbers.add(40);
    System.out.println("LinkedList2: " + numbers);
  }
}

Output

LinkedList1: [Java, C, C++, React]
LinkedList2: [10, 20, 30, 40]

Methods of LinkedList

There are several methods in the LinkedList from which we will see the 4 commonly used methods in detail.

  1. add()
  2. add() method is used for adding the elements at the end of LinkedList in the linked list.

    e.g.

    import java.util.LinkedList;
    class LinkedListAddEx {
      public static void main(String[] args){
      // Creating String LinkedList
        LinkedList<String> languages = new LinkedList<>();
      // Add elements to LinkedList
        languages.add("Java");
        languages.add("C");
        languages.add("C++");
        languages.add("React");
        System.out.println("LinkedList1: " + languages);
        languages.add(2,"JS");
        System.out.println("Modified LinkedList1: " + languages);
      }
    }

    Output

    LinkedList1: [Java, C, C++, React]
    Modified LinkedList1: [Java, C, JS, C++, React]
  3. set()
  4. set() method is used for changing the elements of the LinkedList.

    e.g.

    import java.util.LinkedList;
    class LinkedListSetEx {
      public static void main(String[] args){
      // Creating String LinkedList
        LinkedList<String> languages = new LinkedList<>();
      // Add elements to LinkedList
        languages.add("Java");
        languages.add("C");
        languages.add("C++");
        languages.add("React");
        System.out.println("LinkedList1: " + languages);
        languages.add(2,"Angular");
        System.out.println("Modified LinkedList1: " + languages);
      }
    }

    Output

    LinkedList1: [Java, C, C++, React]
    Modified LinkedList1: [Java, C, Angular, React]
  5. get()
  6. get() method is used for accessing the elements from the LinkedList.

    Syntax

    import java.util.LinkedList;
    class LinkedListGetEx {
      public static void main(String[] args){
      // Creating String LinkedList
        LinkedList<String> languages = new LinkedList<>();
      // Add elements to LinkedList
        languages.add("Java");
        languages.add("C");
        languages.add("C++");
        languages.add("React");
        System.out.println("LinkedList1: " + languages);
        String element = languages.get(3);
        System.out.println("Fetching the element at 3rd index position : " + element);
      }
    }

    e.g.

    LinkedList1: [Java, C, C++, React]
    Fetching the element at 3rd index position: React
  7. remove()
  8. remove()method is used to delete or remove the elements from the Linked List.

    e.g.

    import java.util.LinkedList;
    class LinkedListRemoveEx {
      public static void main(String[] args){
      // Creating String LinkedList
        LinkedList<String> languages = new LinkedList<>();
      // Add elements to LinkedList
        languages.add("Java");
        languages.add("C");
        languages.add("C++");
        languages.add("React");
        System.out.println("LinkedList1: " + languages);
        String element = languages.remove(1);
        System.out.println("Deleted Element : " + element);
        System.out.println("Modified LinkedList1: " + languages);
      }
    }

    Output

    LinkedList1: [Java, C, C++, React]
    Deleted Element : C
    Modified LinkedList1: [Java, C++, React]
  9. indexOf()
  10. indexOf() method returns the index of the first occurrence of the element.

    e.g.

    import java.util.LinkedList;
    class LinkedListIndexOfEx {
      public static void main(String[] args){
      // Creating String LinkedList
        LinkedList<String> languages = new LinkedList<>();
        // Add elements to LinkedList
        languages.add("Java");
        languages.add("C");
        languages.add("C++");
        languages.add("React");
        System.out.println("LinkedList1: " + languages);
        System.out.println("Index of the Element : " + languages.indexOf("React"));
      }
    }

    Output

    LinkedList1: [Java, C, C++, React]
    Index of the Element : 3
  11. lastIndexOf()
  12. lastIndexOf() method returns the index of the last occurrence of the element and returns -1 if there is no occurrence of that element.

    e.g.

    import java.util.LinkedList;
    class LinkedListLastIndexOfEx {
      public static void main(String[] args){
      // Creating String LinkedList
        LinkedList<String> languages = new LinkedList<>();
      // Add elements to LinkedList
      languages.add("Java");
        languages.add("C");
        languages.add("C++");
        languages.add("React");
        languages.add("Java");
        System.out.println("LinkedList1: " + languages);
        System.out.println("Index of the Element : " + languages.lastIndexOf("Java"));
        System.out.println("Index of the Element : " + languages.lastIndexOf("JS"));
      }
    }

    Output

    LinkedList1: [Java, C, C++, React, Java]
      Index of the Element: 4
      Index of the Element: -1
  13. contains()
  14. contains() method check whether the LinkedList contains the element or not.

    e.g.

    import java.util.LinkedList;
    class LinkedListContainsEx {
      public static void main(String[] args){
      // Creating String LinkedList
        LinkedList<String> languages = new LinkedList<>();
      // Add elements to LinkedList
        languages.add("Java");
        languages.add("C");
        languages.add("C++");
        languages.add("React");
        languages.add("Java");
        System.out.println("LinkedList1: " + languages);
        System.out.println("List Contains the element Java : " + languages.contains("Java"));
        System.out.println("List Contains the element JS : " + languages.contains("JS"));
      }
    }

    Output

    LinkedList1: [Java, C, C++, React, Java]
    List Contains the element Java: true
    List Contains the element JS: false
  15. clear()
  16. clear() method removes all the elements from the Linked List.

    e.g.

    import java.util.LinkedList;
    class LinkedListClearEx {
      public static void main(String[] args){
      // Creating String LinkedList
        LinkedList<String> languages = new LinkedList<>();
      // Add elements to LinkedList
        languages.add("Java");
        languages.add("C");
        languages.add("C++");
        languages.add("React");
        languages.add("Java");
        System.out.println("Before clearing the LinkedList1: " + languages);
        languages.clear();
        System.out.println("After clearing the LinkedList1: " + languages);
      }
    }

    Output

    Before clearing the LinkedList1: [Java, C, C++, React, Java]
    After clearing the LinkedList1: [ ]
  17. iterator()
  18. Iterator can be used to loop the LinkedList. If there are more elements in the LinkedList, the hasNext() method returns true, otherwise it returns false. The purpose of the next() method is to return the next element of the linkedList, but if there is no next element then it will throw an exception NoSuchElementException.

    e.g.

    import java.util.LinkedList;
    import java.util.Iterator;
    class LinkedListClearEx {
      public static void main(String[] args){
      // Creating String LinkedList
        LinkedList<String> languages = new LinkedList<>();
      // Add elements to LinkedList
        languages.add("Java");
        languages.add("C");
        languages.add("C++");
        languages.add("React");
        System.out.println("LinkedList1 elements are: ");
        for (Iterator itr = languages.iterator(); itr.hasNext();) {
            System.out.println(itr.next());
        }
      }
    }

    Output

    LinkedList1 elements are:
    Java
    C
    C++
    React
Get in Touch

Atrowel will be pleased to receive your feedback and suggestions. Those of you who would like to contribute, please send us an email.