Difference Between LinkedList and LinkedHashSet in Java



LinkedList and LinkedHashSet both are one of the most important classes of Java Collection framework.

Following are the important differences between LinkedList and LinkedHashSet.

Sr. No.KeyLinkedListLinkedHashSet
1ImplementationLinkedList is the implementation of list and deque interface.LinkedHashSet on other hand is the implementation of set interface and it inherits Hashset class.
2Internal implementationLinkedList internally implements or we can say uses doubly linked list to store the elements.LinkedHashSet on other hand internally uses LinkedHashMap to store it’s elements.
3Order of elementsAs LinkedList internally used doubly linked list so we can add or remove elements from both ends in case of linkedlistWhile LinkedHashset has Hashmap internally so elements could only be inserted at the end of LinkedHashset.
4DuplicatesLinkedList could contains multiple number of duplicates in its collection.On other hand duplicate elements are allowed in LinkedHashSet only like HashSet.
5Index performanceLinkedList Insertion, Removal And Retrieval Operations performance of order O(n)LinkedHashSet also gives performance of order O(1) for insertion, removal and retrieval operations.
6Null AllowedAny number of null value can be inserted in LinkedList without any restriction.On other hand LinkedHashset allows only one null value in its collection,after which no null value is allowed to be added.

Example of LinkedList vs LinkedHashset

JavaTester.java

 Live Demo

import java.util.*;
public class JavaTester {
   public static void main(String args[]){
      LinkedList<String> object = new LinkedList<String>();
      object.add("A");
      object.add("B");
      object.addLast("C");
      object.addFirst("D");
      object.add(2, "E");
      object.add(null);
      object.add(null);
      System.out.println("Linked list : " + object);
      System.out.println("Size of List:" + object.size());
   }
}

Output

Linked list : [D, A, E, B, C, null, null]
Size of List:7

Example

JavaTester.java

 Live Demo

import java.util.LinkedHashSet;
public class JavaTester {
   public static void main(String[] args){
      LinkedHashSet<String> linkedset = new LinkedHashSet<String>();
      linkedset.add("A");
      linkedset.add("B");
      linkedset.add("C");
      linkedset.add("D");
      System.out.println("LinkedHashSet:" + linkedset);
      System.out.println("Size of LinkedHashSet = " + linkedset.size());
      linkedset.add("A");
      System.out.println("After adding duplicate element " + linkedset);
      System.out.println("Size of LinkedHashSet = " + linkedset.size());
      linkedset.add(null);
      linkedset.add(null);
      System.out.println("After adding null element " + linkedset);
      System.out.println("Size of LinkedHashSet = " + linkedset.size());
   }
}

Output

LinkedHashSet:[A, B, C, D]
Size of LinkedHashSet = 4
After adding duplicate element [A, B, C, D]
Size of LinkedHashSet = 4
After adding null element [A, B, C, D, null]
Size of LinkedHashSet = 5

Advertisements