How to print keys with duplicate values in a hashmap?

后端 未结 4 828
天命终不由人
天命终不由人 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<String> 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);
    
    0 讨论(0)
  • 2021-01-21 23:00

    If you want a solution beside to Stream API;

        public static void duplicatedValuesMap() {
            Map<String, String> map = new HashMap<>();
    
            map.put("hello", "0123");
            map.put("hola", "0123");
            map.put("kosta", "0123 test");
            map.put("da", "03");
            map.put("notda", "013");
            map.put("twins2", "01");
            map.put("twins22", "01");
    
            HashMap<String, List<String>> valueToKeyMapCounter = new HashMap<>();
    
            for (Map.Entry<String, String> entry : map.entrySet()) {
                if (valueToKeyMapCounter.containsKey(entry.getValue())) {
                    valueToKeyMapCounter.get(entry.getValue()).add(entry.getKey());
                } else {
                    List<String> keys = new ArrayList<>();
                    keys.add(entry.getKey());
                    valueToKeyMapCounter.put(entry.getValue(), keys);
                }
            }
            for (Map.Entry<String, List<String>> counterEntry : valueToKeyMapCounter.entrySet()) {
                if (counterEntry.getValue().size() > 1) {
                    System.out.println("Duplicated Value:" + counterEntry.getKey() + " for Keys:" + counterEntry.getValue());
                }
            }
    
        }
    
    0 讨论(0)
  • 2021-01-21 23:12

    I think other answers already good to solve the question, i support another method to do just for extended thinking.This method need use Guava's MutliMap interface:

        // init the input map
        Map<String, String> map = new HashMap<>();
        map.put("hello", "0123");
        map.put("hola", "0123");
        map.put("kosta", "0123");
        map.put("da", "03");
        map.put("notda", "013");
        map.put("twins2", "01");
        map.put("twins22", "01");
    
        // swap key and value of the input map,since different key has same value
        // so we need Multimap
        Multimap<String, String> container = ArrayListMultimap.create();
        map.entrySet().forEach(entry -> container.put(entry.getValue(), entry.getKey()));
    
        container.keySet().stream()
            .filter(s -> container.get(s).size() > 1).
            forEach(System.out::println);
    

    output:
    01
    0123

    0 讨论(0)
  • 2021-01-21 23:14

    Build a Map<VALUE, List<KEY>>, i.e. a Map<String, List<String>>.

    Example

    Map<String, String> map = new HashMap<>();
    map.put("hello", "0123");
    map.put("hola", "0123");
    map.put("kosta", "0123");
    map.put("da", "03");
    map.put("notda", "013");
    map.put("twins2", "01");
    map.put("twins22", "01");
    
    map.entrySet().stream()
       .collect(Collectors.groupingBy(Entry::getValue,
                   Collectors.mapping(Entry::getKey, Collectors.toList())))
       .entrySet().stream()
       .filter(e -> e.getValue().size() > 1)
       .forEach(System.out::println);
    

    Output

    01=[twins22, twins2]
    0123=[kosta, hello, hola]
    

    Without the filter(), the result would be:

    01=[twins22, twins2]
    013=[notda]
    03=[da]
    0123=[kosta, hello, hola]
    
    0 讨论(0)
提交回复
热议问题