Java LinkedHashMap
LinkedHashMap implements the Map interface. It inherits the HashMap class. Some essential features of LinkedHashMap are:
- It contains the values based on the keys.
- It maintains the order of insertion.
- It contains unique elements.
- It contains one null key and multiple null values.
- It is not synchronized.
- The initial capacity is 16, and the load factor is 0.75f.
Declaration of LinkedHashMap
LinkedHashMap<String, String> lm= new LinkedHashMap<String, String> ();Constructors in LinkedHashMap:
- LinkedHashMap (): This is used to create default LinkedHashMap constructor.
- LinkedHashMap (int capacity): It is used to set the LinkedHashMap with specified capacity.
- LinkedHashMap (int capacity, float Loadfactor): It is used to set the LinkedHashMap with capacity and Load factor.
- LinkedHashMap (Map m): This is used to set the LinkedHashMap with elements from the given Map.
- LinkedHashMap (int capacity, float Load factor, boolean order): This is used to set the LinkedHashMap with capacity and Loadfactor along with specified order.
Difference between LinkedHashMap and LinkedHashSet:
LinkedHashMap |
LinkedHashSet |
1. LinkedHashMap implements Map interface and inherits HashMap class. | 1. LinkedHashSet implements Set interface and inherits HashSet class. |
2. It replaces the value with duplicate keys. | 2. It does not change the original value. |
3. It was mapping the keys to values. | 3. It simply stores the values. |
4. It enables one null key and multiple null values. | 4. It permits only one null value. |
5. It consists of five types of constructors. | 5. It consists of four types of constructors. |
6.Declaration: LinkedHashMap<Integer, String> lm= new LinkedHashMap<Integer, String> (); | 6.Declaration: LinkedHashSet<String> lh= new LinkedHashSet<String> (); |
Methods of LinkedHashMap:
- clear (): It is used to clear all the mapping from the map.
import java.util.*; public class LinkedDemo { public static void main (String [] args) { LinkedHashMap<Integer, String> l = new LinkedHashMap<Integer, String> (); l.put (10, "Java"); l.put (15, "HTML"); l.put (20, "Python"); l.put (25, "JavaScript"); l.put (30, "C++"); System.out.println ("Initial Mappings are: " + l); l.clear (); System.out.println ("Finally the maps after clear: " + l); } }Output:
Initial Mappings are: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++} Finally the maps after clear: {}
- containsKey (Object K): It returns true if the specified key is present on the map.
import java.util.*; public class LinkedDemo { public static void main (String [] args) { LinkedHashMap<Integer, String> l = new LinkedHashMap<Integer, String>(); l.put (10, "Java"); l.put (15, "HTML"); l.put (20, "Python"); l.put (25, "JavaScript"); l.put (30, "C++"); System.out.println ("Initial Mappings are: " + l); System.out.println ("Is the key '20' present?" + l.containsKey (20)); System.out.println ("Is the key '5' present?" + l.containsKey (5)); } }Output:
Initial Mappings are: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++} Is the key '20' present? true Is the key '5' present? false
- get (Object K): It is used to get the value of a particular key from the map.
import java.util.*; public class LinkedDemo { public static void main (String [] args) { LinkedHashMap<Integer, String> l = new LinkedHashMap<Integer, String>(); l.put (10, "Java"); l.put (15, "HTML"); l.put (20, "Python"); l.put (25, "JavaScript"); l.put (30, "C++"); System.out.println ("Initial Mappings are: " + l); System.out.println ("The value is: " + l.get (20)); System.out.println ("The value is: " + l.get (25)); } }Output:
Initial Mappings are: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++} The value is: Python The value is: JavaScript
- removeEldestEntry (Map.Entry eldest): This method is used to remove the eldest entry from the map.
import java.util.*; public class LinkedDemo { private static final int MAX = 5; public static void main (String [] args) { LinkedHashMap<Integer, String> l = new LinkedHashMap<Integer, String>() { protected boolean removeEldestEntry (Map.Entry<Integer, String> eldest) { return size () > MAX; } }; l.put (0, "Java"); l.put (1, "HTML"); l.put (2, "Python"); l.put (3, "JavaScript"); l.put (4, "C++"); System.out.println ("Initial Mappings are: " + l); l.put (5, "Angularjs"); System.out.println ("" + l); l.put (6, "Reactjs"); System.out.println ("" + l); } }Output:
Initial Mappings are: {0=Java, 1=HTML, 2=Python, 3=JavaScript, 4=C++} {1=HTML, 2=Python, 3=JavaScript, 4=C++, 5=Angularjs} {2=Python, 3=JavaScript, 4=C++, 5=Angularjs, 6=Reactjs}
- forEach (Biconsumer<K, V> action): It completes the given action of each entry in the map until all entries have been processed throws the exception.
import java.util.*; public class LinkedDemo { public static void main (String [] args) { LinkedHashMap<Integer, String> l = new LinkedHashMap<Integer, String>(); l.put (0, "Java"); l.put (1, "HTML"); l.put (2, "Python"); l.put (3, "JavaScript"); l.put (4, "C++"); System.out.println ("Initial Mappings are: " + l); for (Map.Entry m: l.entrySet ()){ System.out.println (m.getKey () +" "+m.getValue ()); } } }Output:
Initial Mappings are: {0=Java, 1=HTML, 2=Python, 3=JavaScript, 4=C++} 0 Java 1 HTML 2 Python 3 JavaScript 4 C++6. keySet (): It returns the set of keys in the map. Example:
import java.util.*; public class LinkedDemo { public static void main (String [] args) { LinkedHashMap<Integer, String> l = new LinkedHashMap<Integer, String>(); l.put (0, "Java"); l.put (1, "HTML"); l.put (2, "Python"); l.put (3, "JavaScript"); l.put (4, "C++"); System.out.println ("Initial Mappings are: " + l); System.out.println ("Keys: "+l.keySet ()); }}Output:
Initial Mappings are: {0=Java, 1=HTML, 2=Python, 3=JavaScript, 4=C++} Keys: [0, 1, 2, 3, 4]
- Values (): This returns the collections of values in the map.
import java.util.*; public class LinkedDemo { public static void main (String [] args) { LinkedHashMap<Integer, String> l = new LinkedHashMap<Integer, String>(); l.put (0, "Java"); l.put (1, "HTML"); l.put (2, "Python"); l.put (3, "JavaScript"); l.put (4, "C++"); System.out.println ("Initial Mappings are: " + l); System.out.println ("Values: "+l.values ()); } }Output:
Initial Mappings are: {0=Java, 1=HTML, 2=Python, 3=JavaScript, 4=C++} Values: [Java, HTML, Python, JavaScript, C++]
- entrySet (): This returns the Set view of the whole mapping in the map.
import java.util.*; public class LinkedDemo { public static void main (String [] args) { LinkedHashMap<Integer, String> l = new LinkedHashMap<Integer, String> (); l.put (0, "Java"); l.put (1, "HTML"); l.put (2, "Python"); l.put (3, "JavaScript"); l.put (4, "C++"); System.out.println ("Initial Mappings are: " + l); System.out.println ("Key-Value pairs: "+l.entrySet ()); } }Output:
Initial Mappings are: {0=Java, 1=HTML, 2=Python, 3=JavaScript, 4=C++} Key-Value pairs: [0=Java, 1=HTML, 2=Python, 3=JavaScript, 4=C++]
- getOrDeafult (Object key, V default Values): This method returns the value which is mapped with particular key. The default value if map cannot contain the mapping of the key.
import java.util.*; public class LinkedDemo { public static void main (String [] args) { LinkedHashMap<Integer, String> l = new LinkedHashMap<Integer, String>(); l.put (0, "Java"); l.put (1, "HTML"); l.put (2, "Python"); l.put (3, "JavaScript"); l.put (4, "C++"); System.out.println ("Initial Mappings are: " + l); System.out.println ("Value pairs by default: "+l.getOrDefault (7,"json")); System.out.println ("Value pairs by default: "+l.getOrDefault (3,"")); } }Output:
Initial Mappings are: {0=Java, 1=HTML, 2=Python, 3=JavaScript, 4=C++} Value pairs by default: json Value pairs by default: JavaScript