How to find the Entry with largest Value in a Java Map


In Java, Map is an object that stores its element in key-value pairs. The Key is an object that is used to fetch and receive value associated with it. The keys must be unique, but the values associated with them may be duplicated depending on the type of Map class we use.

There are several approaches to find an entry with the largest key in Java map and these approaches also depend on the type of Map class we are working with. In this article, we will discuss how to find the entry with largest key in HashMap and TreeMap classes.

Java Program to Find Entry with Largest Key in a Map

In this section, we will discuss the basics of HashMap and TreeMap classes along with the example programs for getting the entry with the largest key.

HashMap Class

It is a generic class that implements Map Interface therefore, it has access to all the methods of this interface. Note that it does not have any additional methods of its own. Duplicate values are not allowed, however, we can store null values and keys. A hash table is used while storing the map.

The general syntax for HashMap is as follows:

Syntax

HashMap<TypeOfKey, TypeOfValue> nameOfMap = new HashMap<>();

Example 1

The following example illustrates how to get the entry with largest key in Java HashMap.

Approach

  • Our first step is to import the necessary packages of 'java.util' so that we can access the functionality of Map.

  • Then, define a HashMap and store a few elements of specified type by using the built-in method 'put()'.

  • Initialize two variables to store entries of largest key.

  • Now, use an inbuilt method 'entrySet()' inside a for-each loop to retrieve the entry of HashMap one by one and then, get the largest entry using the if block.

  • In the end, print the result and exit.

import java.util.Map;
import java.util.HashMap;
public class LargestKey {
   public static void main(String[] args) {
      // Creating a HashMap 
      Map<Integer, Integer> cart = new HashMap<>();
      // Adding elements in the cart map
      cart.put(10, 400);
      cart.put(20, 300);
      cart.put(30, 150);
      cart.put(40, 200);
      cart.put(50, 250);
      // printing all the elements of cart map 
      System.out.println("All elements in the map: " + cart);
      // variables to store the largest key and its value
      int largestKey = 0;
      int largestValue = 0;
      // Loop over all the entries in the Map
      for (Map.Entry<Integer, Integer> entry : cart.entrySet()) {
         // Get the key and value of the current entry
         int key = entry.getKey();
         int value = entry.getValue();
         // Comparing current key with the current largest key
         if (key > largestKey) {
            // Update the largest key and its value
            largestKey = key;
            largestValue = value;
         }
      }
      // Printing the entry with the largest key 
      System.out.println("The entry with the largest key is: [ Quantity: " + largestKey + ", Price: " + largestValue + "]");
   }
}

Output

All elements in the map: {50=250, 20=300, 40=200, 10=400, 30=150}
The entry with the largest key is: [ Quantity: 50, Price: 250]

TreeMap Class

It is a class of Java Collection Framework that implements NavigableMap Interface. It stores the elements of the map in a tree structure to provide an efficient alternative to store the key-value pairs in sorted order.

The general syntax for TreeMap is as follows:

Syntax

TreeMap<TypeOfKey, TypeOfValue> nameOfMap = new TreeMap<>();

Example 2

The following example demonstrates how to get the entry with the largest key in Java TreeMap.

Approach

  • First, import the required packages of 'java.util'.

  • Then, create a TreeMap and store a few elements of specified type by using the built-in method 'put()'.

  • As discussed earlier, a TreeMap stores its elements in a sorted manner therefore the last entry would be largest. Hence, use the inbuilt method 'lastEntry()' to store entries of largest key.

  • Now, retrieve the largest key and its corresponding value from entry.

  • In the end, print the result and exit.

import java.util.Map;
import java.util.TreeMap;
public class LargestKey {
   public static void main(String[] args) {
      // Creating a TreeMap
      TreeMap<Integer, Integer> cart = new TreeMap<>();
      // Adding elements in the cart map
      cart.put(10, 400);
      cart.put(20, 300);
      cart.put(30, 150);
      cart.put(40, 200);
      cart.put(50, 250);
      // printing all the elements of cart map 
      System.out.println("All elements in the map: " + cart);
      // Getting the last entry in the cart map
      Map.Entry<Integer, Integer> entry = cart.lastEntry();
      // Get the key and value of the entry
      int largestKey = entry.getKey();
      int largestValue = entry.getValue();
      // Printing the entry with the largest key
      System.out.println("The entry with the largest key is: [ Quantity: " + largestKey + ", Price: " + largestValue + "]");
   }
}

Output

All elements in the map: {10=400, 20=300, 30=150, 40=200, 50=250}
The entry with the largest key is: [ Quantity: 50, Price: 250]

Conclusion

We started this article by defining Java Map and in the next section, we discussed two approaches to find the entry with largest key from Java Map. Also, we discovered the basics of a few Map classes such as HashMap and TreeMap.

Updated on: 20-Jul-2023

520 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements