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:
- 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 MapLinkedHashSet:
public class LinkedHashSet extends HashSet implements Set, Cloneable, Serializable
Example:
LinkedHashMap
// 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
filter_none
Output:
100 Amit null Anuj 101 Vijay 102 Saurav
LinkedHashSet
// 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
filter_none
Output:
Amit null Vijay Rahul
Recommended Posts:
- LinkedHashMap in Java
- LinkedHashMap get() Method in Java
- LinkedHashMap clear() Method in Java
- LinkedHashMap removeEldestEntry() Method in Java
- LinkedHashMap containsKey() Method in Java
- Differences between TreeMap, HashMap and LinkedHashMap in Java
- LinkedHashSet in Java with Examples
- LinkedHashSet contains() method in Java with Examples
- LinkedHashSet retainAll() method in Java with Example
- LinkedHashSet toString() method in Java with Example
- LinkedHashSet containsAll() method in Java with Example
- LinkedHashSet hashCode() method in Java with Example
- LinkedHashSet equals() method in Java with Example
- LinkedHashSet removeAll() method in Java with Example
- LinkedHashSet toArray(T[]) method in Java with Example
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.



