The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. LinkedHashMap in Java is an implementation that combines HashTable and LinkedList implementation. It implements the Map interface. The key-value pairs of LinkedHashMap have a predictable order of iteration.
We can use the entrySet() method to print all mappings of the LinkedHashMap which returns all the entries contained in the LinkedHashMap object.
Example:
Input:
Key = ["1st-year", "2nd-year", "4th-year"]
Value = ["geeksforgeeks DSA course", "DBMS course",
"Interview prep"]
Output:
1st-year : geeksforgeeks DSA course
2nd-year : DBMS course
4th-year : Interview prep
Input:
Key = [3, 2, 1]
Value = ["geeks", "for", "geeks"]
Output:
3 : geeks
2 : for
1 : geeks
LinkedHashMap(): This is used to construct a default LinkedHashMap constructor.
LinkedHashMap<K, V> l_map = new LinkedHashMap<K, V>();
Here, K is the key Object type and V is the value Object type.
Method’s Syntax:
l_map.entrySet()
Returns: This method returns a Key-Value pair in an ordered manner.
Example 1: The following implementation demonstrates all mappings of the LinkedHashMap using entrySet() method in the key-value pair.
Java
import java.util.*;
class IteratingOverLinkedHashMap {
public static void main(String args[])
{
LinkedHashMap<String, String> l_map
= new LinkedHashMap<String, String>();
l_map.put( "1st-year" , "geeksforgeeks DSA course" );
l_map.put( "2nd-year" , "DBMS course" );
l_map.put( "4th-year" , "Interview prep" );
for (Map.Entry<String, String> mapElement :
l_map.entrySet()) {
String key = mapElement.getKey();
String value = mapElement.getValue();
System.out.println(key + " : " + value);
}
}
}
|
Output
1st-year : geeksforgeeks DSA course
2nd-year : DBMS course
4th-year : Interview prep
Example 2: Another method to print a Key-Value pair is a toString() method of the LinkedHashMap class.
Java
import java.util.*;
class IteratingOverLinkedHashMap {
public static void main(String args[])
{
LinkedHashMap<Integer, String> l_map
= new LinkedHashMap<Integer, String>();
LinkedHashMap<Integer, Integer> r_map
= new LinkedHashMap<Integer, Integer>();
l_map.put( 3 , "geeks" );
l_map.put( 2 , "for" );
l_map.put( 1 , "geeks" );
r_map.put( 3 , 1 );
r_map.put( 2 , 2 );
r_map.put( 1 , 3 );
System.out.println(r_map);
for (Map.Entry<Integer, String> mapElement :
l_map.entrySet()) {
Integer key = mapElement.getKey();
String value = mapElement.getValue();
System.out.println(key + " : " + value);
}
}
}
|
Output
{3=1, 2=2, 1=3}
3 : geeks
2 : for
1 : geeks
Time Complexity: O(n), where n is the number of mappings.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!