LinkedHashMap is a type of Collection which takes all the functionalities of HashMap class i.e. it stores our data in a pair such that each element has a key associated with it. The pair of key and value is often known as Entry. As LinkedHashMap is sub-class of HashMap class so it also indirectly extends AbstractMap class and which further implements Map Interface.
In Addition to all the functionalities of HashMap Class, 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.
Table of Contents
LinkedHashMap Class extends HashMap Class which further extends AbstractMap Class and that Implements Map Interface as shown in following figure 1.
Dotted arrow shows AbstractMap implements Map Interface and Bold Array shows LinkedHashMap extends HashMap which further extends AbstractMap class.
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
Read All LinkedHashMap Methods in JAVA with Example
Let us discuss Linked Hashmap class with the help of program, following program has been divided into 3 Steps that we will discuss one by one.
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>();// Step 1 linkedHashMapobject.put("Samsung Grand quattro price ", new Integer(10000)); // Step 2 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("Contents of LinkedHashMap : " + linkedHashMapobject); //step 3 System.out.println("\nValues of linkedHashMapobject after iterating over it : \n"); for (String key : linkedHashMapobject.keySet()) { //Step 4 System.out.println(key + “:\t” + linkedHashMapobject.get(key)); } } }
Description of Example:
Output:
Contents of LinkedHashMap : {Samsung Grand uattro price =10000, Micromax canvas price=9000, Sony T2 Ultra price=20000, Nokia Lumia price=15000, Microsoft Lumia price =16000} Values of linkedHashMapobject after iterating over it : Samsung Grand uattro price : 10000 Micromax canvas price: 9000 Sony T2 Ultra price: 20000 Nokia Lumia price: 15000 Microsoft Lumia price : 16000
Premium Project Source Code:
Leave a Reply