LinkedHashMap is a type of Collection, which takes all the functionalities of HashMap class. In Addition to them, the functionality of maintaining the insertion is added into LinkedHashMap and to attain this functionality all the entries(key and value) are linked to each other using doubly-linked list. This doubly-linked list maintains the iteration ordering, which is in general the order in which keys were added in the map.
LinkedHashMap Methods in JAVA:
Let us discuss all the LinkedHashMap methods one by one with Examples in Java.
1. void clear()
This method as the name suggests removes all the entries in the LinkedHashMap, as shown in the following program
import java.util.LinkedHashMap; import java.util.Map; public class LinkedHashMapDemo { public static void main(String[] args) { Map<String, Integer> linkedHashMapobject = new LinkedHashMap<String, Integer>(); linkedHashMapobject.put("Samsung Grand quattro price ", new Integer(10000)); linkedHashMapobject.put("Micromax canvas price", new Integer(9000)); linkedHashMapobject.put("Sony T2 Ultra price", new Integer(20000)); linkedHashMapobject.put("Nokia Lumia price", new Integer(15000)); linkedHashMapobject.put("Microsoft Lumia price ", new Integer(16000)); // Displaying the contents of the LinkedHashMap before usig clear method System.out.println("Contents of LinkedHashMap before clear method : " + linkedHashMapobject); linkedHashMapobject.clear(); //Displaying the contents of the LinkedHashMap before usig clear method System.out.println("Contents of LinkedHashMap after clear method: " + linkedHashMapobject); } }
Output:
Contents of LinkedHashMap before clear method: {Samsung Grand quattro price =10000, Micromax canvas price=9000, Sony T2 Ultra price=20000, Nokia Lumia price=15000, Microsoft Lumia price =16000} Contents of LinkedHashMap after clear method: {}
2. boolean containsKey(Object key)
This methods checks whether the key given in the argument list is present or not in the LinkedHashMap entries, as shown in the following program .
import java.util.LinkedHashMap; import java.util.Map; public class LinkedHashMapDemo { public static void main(String[] args) { Map<String, Integer> linkedHashMapobject = new LinkedHashMap<String, Integer>(); linkedHashMapobject.put("Samsung Grand quattro price ", new Integer(10000)); linkedHashMapobject.put("Micromax canvas price", new Integer(9000)); linkedHashMapobject.put("Sony T2 Ultra price", new Integer(20000)); linkedHashMapobject.put("Nokia Lumia price", new Integer(15000)); linkedHashMapobject.put("Microsoft Lumia price ", new Integer(16000)); // Displaying the contents of the LinkedHashMap before usig clear method System.out.println("Contents of LinkedHashMap before : " + linkedHashMapobject); //Checking whether Map contains a particular key System.out.println("\nLinkedHashMap contains 'Nokia Lumia price' as key? : " + linkedHashMapobject.containsKey("Nokia Lumia price")); } }
Output:
Contents of LinkedHashMap before : {Samsung Grand quattro price =10000, Micromax canvas price=9000, Sony T2 Ultra price=20000, Nokia Lumia price=15000, Microsoft Lumia price =16000} LinkedHashMap contains 'Nokia Lumia price' as key? : true
3. boolean containsValue(Object value)
This methods checks whether the value given in the argument list is present or not in the LinkedHashMap entries, as shown in the following program.
import java.util.LinkedHashMap; import java.util.Map; public class LinkedHashMapDemo { public static void main(String[] args) { Map<String, Integer> linkedHashMapobject = new LinkedHashMap<String, Integer>(); linkedHashMapobject.put("Samsung Grand quattro price ", new Integer(10000)); linkedHashMapobject.put("Micromax canvas price", new Integer(9000)); linkedHashMapobject.put("Sony T2 Ultra price", new Integer(20000)); linkedHashMapobject.put("Nokia Lumia price", new Integer(15000)); linkedHashMapobject.put("Microsoft Lumia price ", new Integer(16000)); // Displaying the contents of the LinkedHashMap before usig clear method System.out.println("Contents of LinkedHashMap before : " + linkedHashMapobject); //Checking whether Map contains a particular value System.out.println("LinkedHashMap contains 16000 as value? : " + linkedHashMapobject.containsValue(16000)); } }
Output:
Contents of LinkedHashMap before : {Samsung Grand quattro price =10000, Micromax canvas price=9000, Sony T2 Ultra price=20000, Nokia Lumia price=15000, Microsoft Lumia price =16000} LinkedHashMap contains 16000 as value? : true
4. Object get(Object key)
This method returns the value corresponding to the given key in the argument list , as shown in the following program.
import java.util.LinkedHashMap; import java.util.Map; public class LinkedHashMapDemo { public static void main(String[] args) { Map<String, Integer> linkedHashMapobject = new LinkedHashMap<String, Integer>(); linkedHashMapobject.put("Samsung Grand quattro price", new Integer(10000)); linkedHashMapobject.put("Micromax canvas price", new Integer(9000)); linkedHashMapobject.put("Sony T2 Ultra price", new Integer(20000)); linkedHashMapobject.put("Nokia Lumia price", new Integer(15000)); linkedHashMapobject.put("Microsoft Lumia price ", new Integer(16000)); System.out.println("Price of Samsung Grand quattro::" +linkedHashMapobject.get("Samsung Grand quattro price")); } }
Output:
Price of Samsung Grand quattro ::10000
5. protected boolean removeEldestEntry(Map.Entry eldest)
This method as the name suggests, remove the eldest Entry from the LinkedHashMap.
Now the question is when it should remove the eldest entry
Let us consider a case when we have to store only 4 entries in our linkedHashMap.
We will store 4 in a variable maximumEntries and now every time we try to put any entry into linkedHashMap this methods compares the size of our linkedHashMap to this value, If the value of size comes greater than the predefined size then this methods automatically deletes the eldest element , as shown in the output of the following program.
import java.util.LinkedHashMap; import java.util.Map; public class LinkedHashMapDemo { private static final int maximumEntries= 4; public static void main(String[] args) { LinkedHashMap linkedHashMap = new LinkedHashMap(maximumEntries) { protected boolean removeEldestEntry(Map.Entry eldest) { return size() > maximumEntries; } }; linkedHashMap.put(0, "I"); linkedHashMap.put(1, "L"); linkedHashMap.put(2, "O"); linkedHashMap.put(3, "V"); linkedHashMap.put(4, "E"); linkedHashMap.put(5, "J"); linkedHashMap.put(6, "A"); linkedHashMap.put(7, "V"); linkedHashMap.put(8, "A"); System.out.println("After using removeEldestEntry function:: " + linkedHashMap); } }
Output:
After using removeEldestEntry function:: {5=J, 6=A, 7=V, 8=A}