使用 Java,如何在不重复比较的情况下将 HashMap 中的每个条目与同一 HashMap 中的每个其他条目进行比较?

2022-01-24 00:00:00 iteration hashmap java

我目前正在使用 2 个 for 循环来比较所有条目,但我得到了重复的比较.因为 HashMap 没有排序,所以我不知道如何消除已经进行的比较.例如,我有类似的东西:

I am currently using 2 for loops to compare all entries but I am getting duplicate comparisons. Because HashMaps aren't ordered, I can't figure out how to eliminate comparisons that have already been made. For example, I have something like:

    for(Entry<String, String> e1: map.entrySet())
    { 
        for(Entry<String, String> e2: map.entrySet())
        {    
          if (e1.getKey() != e2.getKey())
            {
           //compare e1.getValue() to e2.getValue() 
            }
        }
     }

这样做的问题是第一个条目将与第二个条目进行比较,然后是第三个条目,依此类推.但随后将再次将第二个条目与第一个条目进行比较,依此类推.然后将第三个条目与第一个条目进行比较,然后是第二个条目,然后是第四个条目,等等.有没有更好的方法来遍历 HashMaps 以避免重复比较?

The problem with this is that the first entry will be compared to the second entry and then the third entry and so on. But then the second entry will again be compared to the first entry and so on. And then the third entry will be compared to the first entry, then the second entry, then the 4th entry, etc. Is there a better way to iterate through HashMaps to avoid doing duplicate comparisons?

附加信息:

更具体地说,希望能回答您的问题,我拥有的 HashMap 存储文件名(键)和文件内容(值) - 只是文本文件.HashMap 已通过遍历包含我要比较的文件的目录来填充.然后我正在做的是通过一些算法运行成对的文件来确定每对文件之间的相似性.我不需要将文件 1 与文件 2 进行比较,然后再将文件 2 与文件 1 进行比较,因为我只需要将这 2 个文件进行一次比较.但我确实需要将每个文件与其他文件进行一次比较.我是使用 HashMaps 的新手.下面的 agim 答案可能只适合我的目的.但我也将尝试围绕以下 Evgeniy Dorofeev 和 Peter Lawrey 的解决方案展开思考.我希望这有助于更好地解释事情.

To be more specific and hopefully answer your questions, the HashMap I have is storing file names (the keys) and file contents (the values) - just text files. The HashMap has been populated by traversing a directory that contains the files I will want to compare. Then what I am doing is running pairs of files through some algorithms to determine the similarity between each pair of files. I do not need to compare file 1 to file 2, and then file 2 to file 1 again, as I only need the 2 files to be compared once. But I do need every file to be compared to every other file once. I am brand new to working with HashMaps. agim’s answer below might just work for my purposes. But I will also try to wrap my brain around both Evgeniy Dorofeev and Peter Lawrey's solutions below. I hope this helps to explain things better.

推荐答案

如果你不小心,消除重复的成本至少会高于对键进行冗余比较的成本.

If you are not careful, the cost of eliminating duplicates could higher than the cost of redundant comparisons for the keys at least.

您可以使用 System.identityHashCode(x)

for(Map.Entry<Key, Value> entry1: map.entrySet()) {
   Key key1 = entry1.getKey();
   int hash1 = System.identityHashCode(key1);
   Value value1 = entry1.getValue();
   for(Map.Entry<Key, Value> entry2: map.entrySet()) {
       Key key2 = entry2.getKey();
       if (key1 > System.identityHashCode(key2)) continue;

       Value value2 = entry1.getValue();
       // compare value1 and value2;
   }
}

相关文章