How to print keys with duplicate values in a hashmap?

后端 未结 4 829
天命终不由人
天命终不由人 2021-01-21 22:25

I have a hashmap with some keys pointing to same values. I want to find all the values that are equal and print the corresponding keys.

This is the current code that I

4条回答
  •  终归单人心
    2021-01-21 23:00

    You can use streams to retrive duplicates in this way:

      List myList = map.stream()
         .filter(n -> Collections.frequency(map.values(), n) > 1)
         .collect(Collectors.toList());
    

    And then, you can print this out with:

    myList.foreach(System.out::println);
    

提交回复
热议问题