Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Java - LinkedList

java_LinkedList

Another concrete implementation of List interface is LinkedList. Unlike ArrayList the elements of LinkedList is not stored in contigous memory locations. It follows the Linked List concept of data structure.


java_LinkedList


Each and every element in a Linked List is called a node. If you see the arrows, the first node knows about the second node and the second node knows about the third node and also the first node. And so on. Its much like a chain. So if you want to go to the last node in the Linked List you need to traverse the entire List.

The methods in the LinkedList are almost same as ArrayList. Off course there are some methods specific to LinkedList. We will check that as well.

Lets see the below example:


public class TestCollection{
  public static void main(String[] arg){

    String s1 = new String("Sham");
    String s2 = new String("Paul");
    String s3 = new String("John");

    // We will be adding these Strings to the LinkedList.
    List lListString = new LinkedList(); // Declare an LinkedList.
    lListString.add(s1); // Add the String to the List.
    lListString.add(s2);
    lListString.add(s3);

    System.out.println(lListString); // Output would be : [Sham,Paul,John]
    String temp = lListString.get(2);
    System.out.println(temp); // Output will be : John
    lListString.remove(1); // Deletes 2nd element from the ArrayList.
    System.out.println(lListString); // New output would be : [Sham,John]

    if(aListString.contains("Sham")){

      System.out.println("Sham is present in the LinkedList.");
      }
    }
    // Starting LinkedList specific methods.
    String s4 = new String("Harry");
    lListString.addFirst(s4);
    System.out.println(lListString); // New output would be : [Harry,Sham,John]
    String s5 = new String("Tom");
    lListString.addLast(s5);
    System.out.println(lListString); // New output would be : [Harry,Sham,John,Tom]
  }
}


If you see the above lines of code, the methods are almost same as the ArrayList. Except


1) lListString.addFirst(s4);

As the name of the method is, the element would be added at the beginning of the LinkedList. And remember, 'addFirst()' this method is specific to LinkedList.


2) lListString.addLast(s5);

Even for this method we can guess, the element would be added at the end of the List.


Advantages and Disadvantages of LinkedList :

1) lListString.add(s1) :  Addition takes very less time. As the element is added at
            the end of the List.

2) lListString.get(2) :  Fetching an element is not good in LinkedList as traversal
          through the entire List is needed.

3) lListString.remove(1) :  Removal is better compared to ArrayList as no shifting
            operation is needed.

4) lListString.contains("Sham") :  Same as ArrayList.

Adding custom objects to a LinkedList

We will be creating a Human class and adding its objects to the LinkedList.


Human Class

class Human {

  int age;
  String name;

  public Human(int age, String name){
    this.age = age;
    this.name = name;
  }

  -- getters & setters --

}

Now, let us define the class with main() method.


public class TestCollection{
  public static void main(String[] arg){

    Human human1 = new Human(34,"Tom");
    Human human2 = new Human(21,"Harry");
    Human human3 = new Human(27,"Mona");
    Human human4 = new Human(43,"Lily");
    List <Human> listHuman = new LinkedList <Human> ();
    listHuman.add(human1);
    listHuman.add(human2);
    listHuman.add(human3);
    listHuman.add(human4); // All the Humans are added to the List.

  }
}


In the above example we have added four 'Human' objects and added those to the List.


listHuman.add(human1);
listHuman.add(human2);
listHuman.add(human3);
listHuman.add(human4);