LinkedHashMap and LinkedHashSet in Java

Similarity Between LinkedHashMap and LinkedHashSet:

  • Order: Both LinkedHashMap and LinkedHashSet maintain the insertion order. Elements get sorted in the same sequence in which they have been added.
  • Synchronized: Both are not synchronized and must be synchronized externally.
  • Duplicates: LinkedHashMap does a mapping of keys to values so not have duplicates and LinkedHashSet simply stores a collection of things with no duplicates.
  • Memory: Keeping the insertion order in both LinkedHashmap and LinkedHashset have additional associated costs, both in terms of spending additional CPU cycles and needing more memory.

Difference between linkedhashmap and linkedhashset:

  • Inheritance:
    LinkedHashMap and LinkedHashSet
  • Replacement: LinkedHashMap replace value with duplicate key while LinkedHashSet not change original value.
  • Operation: LinkedHashMap does a mapping of keys to values while LinkedHashSet simply stores a collection of things.
  • Null Object: LinkedHashMap have elements in key value pair so have only one null key and multiple null value, while LinkedHashSet simply stores a collection of things with one null value.
  • Constructor: The LinkedHashMap accepts five types of constructors:
    LinkedHashMap(), LinkedHashMap(int capacity), LinkedHashMap(int capacity, float fillRatio),
    LinkedHashMap(int capacity, float fillRatio, boolean Order)

    The LinkedHashSet accepts four types of constructors:
    HashSet(), HashSet(Collection C), LinkedHashSet(int size),
    LinkedHashSet(int capacity, float fillRatio)

  • Declaration:

    LinkedHashMap:

    LinkedHashMap lhm = new LinkedHashMap();

    LinkedHashSet:



    LinkedHashSet hs = new LinkedHashSet();
  • Class Declaration:

    LinkedHashMap:

    public class LinkedHashMap extends HashMap implements Map

    LinkedHashSet:

    public class LinkedHashSet extends HashSet implements Set, Cloneable, Serializable

Example:

LinkedHashMap

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working of 
// LinkedHashMap
import java.util.*;
  
class LinkedHashMapTest {
  
    public static void main(String args[])
    {
  
        LinkedHashMap<Integer, String> lhm;
        lhm = new LinkedHashMap<Integer, String>();
  
        // insert element in LinkedHashMap
        lhm.put(100, "Amit");
  
        // inser first null key
        lhm.put(null, "Ajay");
        lhm.put(101, "Vijay");
        lhm.put(102, "Rahul");
  
        // insert second null key
        // which replace first null key value
        lhm.put(null, "Anuj");
  
        // insert duplicate
        // which replace first 102 key value
        lhm.put(102, "Saurav");
  
        lhm.entrySet().stream().forEach((m) -> {
            System.out.println(m.getKey() + " " + m.getValue());
        });
    }
}

chevron_right


Output:

100 Amit
null Anuj
101 Vijay
102 Saurav

LinkedHashSet

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working of 
// LinkedHashSet
import java.util.*;
  
class LinkedHashMapTest {
  
    public static void main(String args[])
    {
        LinkedHashSet<String> lhs = new LinkedHashSet<String>();
  
        // insert element in LinkedHashMap
        lhs.add("Amit");
  
        // insert first null key
        lhs.add(null);
        lhs.add("Vijay");
        lhs.add("Rahul");
  
        // insert second null key
        // which replace first null key value
        lhs.add(null);
        // insert duplicate
        lhs.add("Vijay");
  
        Iterator<String> itr = lhs.iterator();
        while (itr.hasNext()) {
            System.out.println(itr.next());
        }
    }
}

chevron_right


Output:

Amit
null
Vijay
Rahul

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.