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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/hashmap("`HashMap`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/if_else("`If...Else`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/DataStructuresGroup -.-> java/collections_methods("`Collections Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117436{{"`How to Find Maximum Value Map`"}} java/classes_objects -.-> lab-117436{{"`How to Find Maximum Value Map`"}} java/class_methods -.-> lab-117436{{"`How to Find Maximum Value Map`"}} java/hashmap -.-> lab-117436{{"`How to Find Maximum Value Map`"}} java/modifiers -.-> lab-117436{{"`How to Find Maximum Value Map`"}} java/oop -.-> lab-117436{{"`How to Find Maximum Value Map`"}} java/wrapper_classes -.-> lab-117436{{"`How to Find Maximum Value Map`"}} java/identifier -.-> lab-117436{{"`How to Find Maximum Value Map`"}} java/data_types -.-> lab-117436{{"`How to Find Maximum Value Map`"}} java/if_else -.-> lab-117436{{"`How to Find Maximum Value Map`"}} java/operators -.-> lab-117436{{"`How to Find Maximum Value Map`"}} java/output -.-> lab-117436{{"`How to Find Maximum Value Map`"}} java/strings -.-> lab-117436{{"`How to Find Maximum Value Map`"}} java/collections_methods -.-> lab-117436{{"`How to Find Maximum Value Map`"}} java/system_methods -.-> lab-117436{{"`How to Find Maximum Value Map`"}} end

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