Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaHow to Print all Mappings of the LinkedHashMap in Java?

How to Print all Mappings of the LinkedHashMap in Java?

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 = ["neveropen DSA course", "DBMS course", 
"Interview prep"]

Output:
1st-year : neveropen 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




// Java program to Print all 
// Mappings of the LinkedHashMap
import java.util.*;
  
class IteratingOverLinkedHashMap {
    public static void main(String args[])
    {
  
        // create an instance of LinkedHashMap
        LinkedHashMap<String, String> l_map
            = new LinkedHashMap<String, String>();
  
        // Add mappings using put method
        l_map.put("1st-year", "neveropen DSA course");
        l_map.put("2nd-year", "DBMS course");
        l_map.put("4th-year", "Interview prep");
  
        // retrieve the key-value pairs as set using
        // entrySet & print each entry
        for (Map.Entry<String, String> mapElement :
             l_map.entrySet()) {
  
            // Finding the key
            String key = mapElement.getKey();
  
            // Finding the value
            String value = mapElement.getValue();
  
            // print the key : value pair
            System.out.println(key + " : " + value);
        }
    }
}


Output

1st-year : neveropen 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




// Java program to Print all 
// Mappings of the LinkedHashMap
import java.util.*;
  
class IteratingOverLinkedHashMap {
    public static void main(String args[])
    {
  
        // create an instance of LinkedHashMap
        LinkedHashMap<Integer, String> l_map
            = new LinkedHashMap<Integer, String>();
        LinkedHashMap<Integer, Integer> r_map
            = new LinkedHashMap<Integer, Integer>();
  
        // Add mappings using put method
        l_map.put(3, "geeks");
        l_map.put(2, "for");
        l_map.put(1, "geeks");
  
        // Add mappings using put method
        r_map.put(3, 1);
        r_map.put(2, 2);
        r_map.put(1, 3);
  
        // The toString method returns all mappings of
        // map where keys and values are separated by a =,
        // each mapping is separated by a comma and all
        // mappings are enclosed in { and }.
        System.out.println(r_map);
  
        // retrieve the key-value pairs as set using
        // entrySet & print each mapElement
        for (Map.Entry<Integer, String> mapElement :
             l_map.entrySet()) {
  
            // Finding the key
            Integer key = mapElement.getKey();
  
            // Finding the value
            String value = mapElement.getValue();
  
            // print the key : value pair
            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.

RELATED ARTICLES

Most Popular

Recent Comments