We have seen all the details of HashMap in java and the Internal working of HashMap. In this post, we will see LinkedHashMap in java and LinkedHashMap example. The LinkedHashMap class is part of the Collection framework.
Here is the table content of the article will we will cover this topic.
1. LinkedHashMap in java?
2. Important point about LinkedHashMap in java?
3. How to create LinkedHashMap in java?
4. Constructor of LinkedHashMap in java?
5. Methods of LinkedHashMap?
6. Difference between HashMap and LinkedHashMap?
LinkedHashMap in java?
LinkedHashMap in java is very similar to HashMap class but it maintains the insertion order of the object in which those inserted. It extends the HashMap class and implements the Map interface.
The LinkedHashMap uses the Hash table and linked list implementation to store and perform operations. A HashMap doesn’t use LinkedList data structure, so it differs from HashMap and maintains a doubly-linked list. All the entries of LinkedHashMap use the doubly LinkedList structure to maintains the order of objects. It maintains the insertion order of object and links them by use of LinkedList. But we can change the iteration order of element by use of a special constructor that can change the iteration order from the least-recently accessed element to the most-recently accessed element and vice versa.
LinkedHashMap<K, V> hashMapData = new LinkedHashMap <K, V>();
Here K, is the type of type data that you want to maintain in LinkedHashMap.
V, the type of Value that you want to store in LinkedHashMap.
Important point about LinkedHashMap in java
1. In LinkedHashMap each value stored with unique key as like in HashMap.
2. LinkedHashMap can contains duplicate value but it can’t contain duplicate key.
3. LinkedHashMap can contains multiple null values but it can contain only one null key.
4. You can get value from LinkedHashMap by use of unique Key.
5. LinkedHashMap maintains the insertion order.
6. LinkedHashMap class is non-synchronized.
7. LinkedHashMap has default initial capacity is 16 and load factor is 0.75.
How to create LinkedHashMap in java?
We can create LinkedHashMap in java by use of the constructor of LinkedHashMap. There are some constructors that create an object of LinkedHashMap.
Constructor of LinkedHashMap in java
1. LinkedHashMap(): It is the default Constructor of LinkedHashMap class that creates an empty object of LinkedHashMap. It has a default capacity 16 and a load factor is 0.75. Let’s see the java linkedhashmap example.
import java.util.LinkedHashMap; public class LinkedHashMapExample { public static void main(String args[]) { LinkedHashMap<Integer, String> linkedHashMap = new LinkedHashMap<Integer, String>(); linkedHashMap.put(1, "Hi"); linkedHashMap.put(2, "Hello"); linkedHashMap.put(3, "Bye"); linkedHashMap.put(6, "Good"); linkedHashMap.put(5, "JavaGoal"); linkedHashMap.put(4, "Learning"); System.out.println("Object from LinkedHashMap: "+linkedHashMap); } }
Output: Object from LinkedHashMap: {1=Hi, 2=Hello, 3=Bye, 6=Good, 5=JavaGoal, 4=Learning}
2. LinkedHashMap(int initialCapacity): This constructor is used to create an object with specified capacity. It creates an empty object with a default load factor 0.75.
import java.util.LinkedHashMap; public class LinkedHashMapExample { public static void main(String args[]) { LinkedHashMap<Integer, String> linkedHashMap = new LinkedHashMap<Integer, String>(8); linkedHashMap.put(1, "Hi"); linkedHashMap.put(2, "Hello"); linkedHashMap.put(3, "Bye"); linkedHashMap.put(6, "Good"); linkedHashMap.put(5, "JavaGoal"); linkedHashMap.put(4, "Learning"); System.out.println("Object from LinkedHashMap: "+linkedHashMap); } }
Output: Object from LinkedHashMap: {1=Hi, 2=Hello, 3=Bye, 6=Good, 5=JavaGoal, 4=Learning}
3. LinkedHashMap(int initialCapacity, float loadFactor): This constructor is used to create an object with specified capacity and specified load factor. It creates an empty object.
import java.util.LinkedHashMap; public class LinkedHashMapExample { public static void main(String args[]) { LinkedHashMap<Integer, String> linkedHashMap = new LinkedHashMap<Integer, String>(8, 0.80f); linkedHashMap.put(1, "Hi"); linkedHashMap.put(2, "Hello"); linkedHashMap.put(3, "Bye"); linkedHashMap.put(6, "Good"); linkedHashMap.put(5, "JavaGoal"); linkedHashMap.put(4, "Learning"); System.out.println("Object from LinkedHashMap: "+linkedHashMap); } }
Output: Object from LinkedHashMap: {1=Hi, 2=Hello, 3=Bye, 6=Good, 5=JavaGoal, 4=Learning}
4. LinkedHashMap (int initialCapacity, float loadFactor, boolean accessOrder) This constructor is used to create an object of LinkedHashMap with specified capacity, load factor, and order of objects. We can maintain the access order of objects from least recently accessed used to most recently accessed.
import java.util.LinkedHashMap; public class LinkedHashMapExample { public static void main(String args[]) { LinkedHashMap<Integer, String> linkedHashMap = new LinkedHashMap<Integer, String>(8, 0.80f, true); linkedHashMap.put(1, "Hi"); linkedHashMap.put(2, "Hello"); linkedHashMap.put(3, "Bye"); linkedHashMap.put(6, "Good"); linkedHashMap.put(5, "JavaGoal"); linkedHashMap.put(4, "Learning"); System.out.println("Object from LinkedHashMap: "+linkedHashMap); } }
Output: Object from LinkedHashMap: {1=Hi, 2=Hello, 3=Bye, 6=Good, 5=JavaGoal, 4=Learning}
5. LinkedHashMap(Map m): This constructor creates an object of LinkedHashMap that hold all the object from the specified map. It contains all entries from the map and also maintains the same load factor and access order. This is the one way to create a LinkedHashMap by use of Map.
import java.util.HashMap; import java.util.LinkedHashMap; public class LinkedHashMapExample { public static void main(String args[]) { HashMap<Integer, String> map = new HashMap<Integer, String>(); map.put(1, "Hi"); map.put(2, "Hello"); map.put(3, "Bye"); map.put(6, "Good"); map.put(5, "JavaGoal"); map.put(4, "Learning"); LinkedHashMap<Integer, String> linkedHashMap = new LinkedHashMap<Integer, String>(map); System.out.println("Object from LinkedHashMap: "+linkedHashMap); } }
Output: Object from LinkedHashMap: {1=Hi, 2=Hello, 3=Bye, 4=Learning, 5=JavaGoal, 6=Good}
Methods of LinkedHashMap
1. get(Object key) method
This method is used to get or fetch the value mapped by the specified key. It returns the value if the specified key is mapped but if LinkedHashMap hasn’t mapped for the key then it returns NULL.
get(Object key);
Where, Key is the key with which the specified value(V) is to be associated in LinkedHashMap.
return NULL, if there was no mapping for key otherwise it returns the value that associated with specified key.
import java.util.LinkedHashMap; public class ExampleOfLinkedHashMap { public static void main(String[] args) { LinkedHashMap <Integer, String> listOfNames = new LinkedHashMap <Integer, String>(); listOfNames.put(1, "JAVA"); listOfNames.put(2, "GOAL"); listOfNames.put(3, "SITE"); for(Integer key : listOfNames.keySet()) System.out.println("Names from list = "+listOfNames.get(key)); } }
Output: Names from list = JAVA
Names from list = GOAL
Names from list = SITE
2. getOrDefault(Object key, V defaultValue) method
This method is used to get or fetch the value mapped by specified key, or defaultValue if this map contains no mapping for the key.
getOrDefault(Object key, V defaultValue)
Where, Key is the key with which the specified value(V) is to be associated in LinkedHashMap.
defaultValue, if LinkedHashMap doesn’t contains mapping for the key.
import java.util.LinkedHashMap; public class ExampleOfLinkedHashMap { public static void main(String[] args) { LinkedHashMap <Integer, String> listOfNames = new LinkedHashMap <Integer, String>(); listOfNames.put(1, "JAVA"); listOfNames.put(2, "GOAL"); listOfNames.put(3, "SITE"); // Here 1 is key in LinkedHashMap so it doesn't return default value System.out.println("Names from list = "+listOfNames.getOrDefault(1, "Hello")); // Here 5 is key in LinkedHashMap which doesn't exist So it returns default value System.out.println("Names from list = "+listOfNames.getOrDefault(5, "Hello")); } }
Output: Names from list = JAVA
Names from list = Hello
3. containsKey(Object key) method
This method is used to check whether the specified key exists in LinkedHashMap or not. It returns type is boolean. If the given key exists in LinkedHashMap, then it returns true otherwise false.
containsKey(Object key);
Where, Key is the key with which the specified value(V) is to be associated in LinkedHashMap.
return type: Its return type is boolean. It can return either true or false.
import java.util.LinkedHashMap; public class ExampleOfLinkedHashMap { public static void main(String[] args) { LinkedHashMap<Integer, String> listOfNames = new LinkedHashMap<Integer, String>(); listOfNames.put(1, "JAVA"); listOfNames.put(2, "GOAL"); listOfNames.put(3, "RAVI"); // Return, true because key 2 is exist System.out.println("Is key 2 is present in LinkedHashMap = "+listOfNames.containsKey(2)); // Return, false because key 5 doesn't exist System.out.println("Is key 5 is present in LinkedHashMap = "+listOfNames.containsKey(5)); } }
Output: Is key 2 is present in LinkedHashMap = true
Is key 5 is present in LinkedHashMap = false
4. containsValue(Object value) method
This method is used to check whether the specified Value exists in LinkedHashMap or not. It returns type is boolean. If the given value exists in LinkedHashMap, then it returns true otherwise false.
containsValue(Object value)
Where, Value is the value which you want to check in LinkedHashMap.
return type: Its return type is boolean. It can return either true or false.
import java.util.LinkedHashMap; public class ExampleOfLinkedHashMap { public static void main(String[] args) { LinkedHashMap<Integer, String> listOfNames = new LinkedHashMap<Integer, String>(); listOfNames.put(1, "JAVA"); listOfNames.put(2, "GOAL"); listOfNames.put(3, "RAVI"); // Return, true because value JAVA is exist System.out.println("Is Value JAVA is present in LinkedHashMap = "+listOfNames.containsValue("JAVA")); // Return, false because value Hello doesn't exist System.out.println("Is Value Hello is present in LinkedHashMap = "+listOfNames.containsValue("Hello")); } }
Output: Is Value JAVA is present in LinkedHashMap = true
Is Value Hello is present in LinkedHashMap = false
5. clear() method
This method is used to remove all the elements from the LinkedHashMap. Its return type is void. It means this method doesn’t return anything.
void clear();
import java.util.LinkedHashMap; public class ExampleOfLinkedHashMap { public static void main(String[] args) { LinkedHashMap<Integer, String> listOfNames = new LinkedHashMap<Integer, String>(); listOfNames.put(1, "JAVA"); listOfNames.put(2, "GOAL"); listOfNames.put(3, "RAVI"); // Before clear() method for(Integer key : listOfNames.keySet()) { System.out.println("Name from list = "+listOfNames.get(key)); } // Removing all the element by clear() method listOfNames.clear(); System.out.println("After Clear() method list is empty : "+ listOfNames.isEmpty()); } }
Output: Name from list = JAVA
Name from list = GOAL
Name from list = RAVI
After Clear() method list is empty : true
6. keySet() method
This method is used to fetch the Set view of all keys. It returns all the key set. This Set worked on backed of LinkedHashMap, if any change makes in LinkedHashMap also reflects in this Set and vice versa. It returns type is Set.
Set<K> keySet();
Where, K represents the type keys in LinkedHashMap.
import java.util.LinkedHashMap; public class ExampleOfLinkedHashMap { public static void main(String[] args) { LinkedHashMap<Integer,String> listOfNames = new LinkedHashMap<Integer, String>(); listOfNames.put(1, "JAVA"); listOfNames.put(2, "GOAL"); listOfNames.put(3, "RAVI"); System.out.println("Key set of LinkedHashMap = "+listOfNames.keySet()); } }
Output: Key set of LinkedHashMap = [1, 2, 3]
7. entrySet() method
This method is used to fetch the Set view of all keys and Values. It returns all the key and values in form of Set. This Set worked on backed of LinkedHashMap, if any change makes in LinkedHashMap also reflects in this Set and vice versa. It returns type is Set.
Set<Map.Entry<K,V>> entrySet();
Where, K represents the type Keys in LinkedHashMap.
V represents the type Values in LinkedHashMap.
import java.util.LinkedHashMap; public class ExampleOfLinkedHashMap { public static void main(String[] args) { LinkedHashMap<Integer,String> listOfNames = new LinkedHashMap<Integer, String>(); listOfNames.put(1, "JAVA"); listOfNames.put(2, "GOAL"); listOfNames.put(3, "RAVI"); System.out.println("Keys and values set of LinkedHashMap = "+listOfNames.entrySet()); } }
Output: Keys and values set of LinkedHashMap = [1=JAVA, 2=GOAL, 3=RAVI]
8. values() method
This method is used to get all values of LinkedHashMap. It returns type is Collection. It returns Collection that contains all the values of LinkedHashMap.
Collection<V> values();
Where, V represents the type Values in LinkedHashMap.
import java.util.Collection; import java.util.LinkedHashMap; public class ExampleOfLinkedHashMap { public static void main(String[] args) { LinkedHashMap<Integer,String> listOfNames = new LinkedHashMap<Integer, String>(); listOfNames.put(1, "JAVA"); listOfNames.put(2, "GOAL"); listOfNames.put(3, "RAVI"); Collection<String> values = listOfNames.values(); System.out.println("Values of LinkedHashMap = "+values); } }
Output: Values of LinkedHashMap = [JAVA, GOAL, RAVI]