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. Key LinkedList LinkedHashSet
1 Implementation LinkedList is the implementation of list and deque interface. LinkedHashSet on other hand is the implementation of set interface and it inherits Hashset class.
2 Internal Implementation LinkedList 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.
3 Order of Elements As LinkedList internally used doubly linked list so we can add or remove elements from both ends in case of linkedlist. While LinkedHashset has Hashmap internally so elements could only be inserted at the end of LinkedHashset.
4 Duplicates LinkedList could contains multiple number of duplicates in its collection. On other hand duplicate elements are allowed in LinkedHashSet only like HashSet.
5 Index Performance LinkedList Insertion, Removal And Retrieval Operations performance of order O(n). LinkedHashSet also gives performance of order O(1) for insertion, removal and retrieval operations.
6 Null Allowed Any 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.

LinkedList vs LinkedHashset

Example

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

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

Updated on: 07-Dec-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements