1public class NewClass4 {
2 public static void main(String[] args)
3 {
4 HashMap<Integer,Integer>map=new HashMap<Integer, Integer>();
5 map.put(1, 50);
6 map.put(2, 60);
7 map.put(3, 30);
8 map.put(4, 60);
9 map.put(5, 60);
10 int maxValueInMap=(Collections.max(map.values())); // This will return max value in the Hashmap
11 for (Entry<Integer, Integer> entry : map.entrySet()) { // Itrate through hashmap
12 if (entry.getValue()==maxValueInMap) {
13 System.out.println(entry.getKey()); // Print the key with max value
14 }
15 }
16
17 }
18}
1fun max(map:Map<Int,String>) = map.maxByOrNull { it.key }?.key ?:-1//returns negative one if the max is null
2fun main(){
3var map = mapOf(1 to "hello",2 to "peace", 3 to "ochuko")
4 println(map.maxByOrNull { it.key }?.key ?:-1)
5}
1//For kotlin
2fun max(map:Map<Int,String>) = map.maxByOrNull { it.key }?.key ?:-1//returns negative one if the max is null
3fun main(){
4var map = mapOf(1 to "hello",2 to "peace", 3 to "ochuko")
5 println(map.maxByOrNull { it.key }?.key ?:-1)
6}