How to Find Maximum Value Map

JavaJavaBeginner
Practice Now

Introduction

In Java, the Map data structure is used to store key-value pairs. It is challenging to find the maximum value in a map since it does not store the data in a sequence. In this lab, you will learn how to find the maximum value in a Java Map using the iterative approach and the Collections class.

Create Java Map

Create a Java program that creates a Map of prices for every corresponding course in key-value format.

import java.util.*;

public class MaxValueInMap {
    public static void main(String args[]) {
        Map<String, Integer> coursePrice = new HashMap<>();
        coursePrice.put("Java", 5000);
        coursePrice.put("Python", 3000);
        coursePrice.put("CPP", 4000);
        coursePrice.put("Android", 8000);
        System.out.println(coursePrice);
    }
}

Save the file and run the program using the following command:

javac MaxValueInMap.java && java MaxValueInMap

Find Maximum Value Iteratively

Iterate over all the values of the map to find the maximum value. For this, create a null Entry of Map type and then iterate over all the values of the map. Whenever you get a bigger value or null value we assign it to Entry and at the end simply print the value of Entry.

import java.util.*;

public class MaxValueInMap {
    public static void main(String args[]) {
        Map<String, Integer> coursePrices = new HashMap<>();
        Map.Entry<String, Integer> maxPrice = null;

        coursePrices.put("Java", 5000);
        coursePrices.put("Python", 3000);
        coursePrices.put("CPP", 4000);
        coursePrices.put("Android", 8000);

        for (Map.Entry<String, Integer> price : coursePrices.entrySet()) {
            if (maxPrice == null || price.getValue().compareTo(maxPrice.getValue()) > 0) {
                maxPrice = price;
            }
        }

        System.out.println("Maximum price is: " + maxPrice.getValue());
    }
}

Save the file and run the program using the following command:

javac MaxValueInMap.java && java MaxValueInMap

Find Maximum Value using Collections.max()

Here, we can use the Collections.max() method to find the maximum value. The Collections.max() method finds and returns the maximum element present in the collection. In this case, we will pass Map.values().

import java.util.*;

public class MaxValueInMap {
    public static void main(String args[]) {
        Map<String, Integer> coursePrice = new HashMap<>();
        coursePrice.put("Java", 5000);
        coursePrice.put("Python", 3000);
        coursePrice.put("CPP", 4000);
        coursePrice.put("Android", 8000);
        System.out.println("maximum price is : "+ Collections.max(coursePrice.values()));
    }
}

Save the file and run the program using the following command:

javac MaxValueInMap.java && java MaxValueInMap

Find Maximum Key using Collections.max()

If we want to find the maximum key instead of values, then we need to pass the method Map.keySet() instead of Map.values() to the Collections.max() method. In this case, all the course prices are keys and course names are values.

import java.util.*;

public class MaxValueInMap {
    public static void main(String args[]) {
        Map<Integer, String> coursePrice = new HashMap<>();
        coursePrice.put(5000, "Java");
        coursePrice.put(3000, "Python");
        coursePrice.put(4000, "CPP");
        coursePrice.put(8000, "Android");
        System.out.println("maximum price is : " + Collections.max(coursePrice.keySet()));
    }
}

Save the file and run the program using the following command:

javac MaxValueInMap.java && java MaxValueInMap

Summary

In this lab, you have learned how to find the maximum value and maximum key in Java Map. You implemented the iterative approach and used the Collections class to find the maximum value in a Java Map. Keep practicing to strengthen your Java programming skills.

Other Java Tutorials you may like