M
M
Misha23102022-04-01 12:04:03
Java
Misha2310, 2022-04-01 12:04:03

How to print the value of a key knowing the value of value in Java hashmap?

I created a HashMap with 3 values. It is necessary that the program displays the String key behind the maximum Integer value, using iteration of the value value, translates it into a LinkedList, and finds the maximum value through if. I know, it's trite, probably, but the main thing is to cope with the task. So here's how to output the key of a String after a certain index. As for value, you can use get, but for key I did not find a similar way. I will be very grateful for your support!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2022-04-01
@Misha2310

var stringIntegerMap = Map.of("1", 1,
                "2", 2,
                "3", 3);

        var max = stringIntegerMap.entrySet().stream()
                .max(Map.Entry.comparingByValue())
                .orElseThrow();

        System.out.println(max.getKey());
        System.out.println(max.getValue());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question