How To Find Maximum Value in Java Map
In this article, we will learn to sort elements of Java Map. Map stores data in a key-value pair format and on top of that it stores in random locations, that's why it is hard to find Max values in Map in Java.
Challenges To Finding Max Value In Java Map
- Map in Java stores value in random locations.
- Since it stores data in the key-value format it is necessary to decide which factor should consider for comparison.
Let's understand by a program using Map
in Java. In the program given below, we created a Map
that will store the price for every corresponding course in key-value format.
import java.util.*;
public class StudyTonight
{
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);
}
}
Do not forget to import java.util.*;
package otherwise compiler will give an error.
{Java=5000, CPP=4000, Python=3000, Android=8000}
Now, it is very obvious to think Which course is the cheapest? Which course is expensive?
In a programming world, we tackle these situations by simply finding the largest and smallest value. Before moving to the actual solutions let's discuss iterative solutions.
Iterative Solution to Find Maximum Value in Map
In this program, we created a null Entry of Map type and then iterate over all the values of the map. Whenever we 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 StudyTonight
{
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());
}
}
Maximum price is: 8000
Example of finding Maximum Value in Map
Here, we simply passed all the values of Map using Map.values()
and other stuff is automatically done by Collections
class in Java.
This example will print the maximum price of course present on the Map.
import java.util.*;
public class StudyTonight
{
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()));
}
}
maximum price is: 8000
What if we want to find the maximum Map key instead of values?
In that case, we need to just pass the method Map.keySet() instead of Map.values(), the code to implement for this case is given below where all the course prices are keys and course names are values. So, we passed Map.keySet() in Collection.max() method.
import java.util.*;
public class StudyTonight
{
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()));
}
}
maximum price is : 8000
Conclusion:
In this article, we learned to find max value in Java Map by using the Collections
class or conventional iterative comparison method. Map provides O(1) for accessing time but O(n) for finding maximum element from it.