如何在哈希表中打印具有重复值的键?

3

我有一个哈希映射表,其中一些键指向相同的值。我想找到所有相等的值,并打印相应的键。

这是我当前的代码:

    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");


    List<String> myList = new ArrayList<>();

    for (Map.Entry<String, String> entry : map.entrySet()) {
       for (Map.Entry<String, String> entry2 : map.entrySet()){
           if (entry.getValue().equals(entry2.getValue()))
           {
               myList.add(entry.getKey());
           }
       }

    }

当前代码将重复项添加两次到列表中,但它也会添加每个键一次。

谢谢。


1
https://dev59.com/d14c5IYBdhLWcg3wqLxe#30741906 - khachik
对每个条目进行迭代。将值添加到一个新Set中,并检查该值是否已包含在其中。这保证了O(n)的运行时间。 - Glains
如果你关心性能的话,最好再有一个HashMap来将你的值作为键映射到原始HashMap中的键数组或列表。 - Pavel Smirnov
4个回答

2
您可以使用流来以以下方式检索重复项:
  List<String> myList = map.stream()
     .filter(n -> Collections.frequency(map.values(), n) > 1)
     .collect(Collectors.toList());

然后,您可以使用以下方法将其打印出来:
myList.foreach(System.out::println);

0
构建一个Map<VALUE,List<KEY>>,即一个 Map<String,List<String>>

示例

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);

输出

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

如果没有使用filter(),结果将会是:

01=[twins22, twins2]
013=[notda]
03=[da]
0123=[kosta, hello, hola]

0
如果您想要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

我认为其他答案已经很好地解决了这个问题,我支持另一种方法,只是为了扩展思路。这种方法需要使用GuavaMutliMap接口:

    // 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);

输出:
01
0123


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接