使用Java,如何在不重复比较的情况下将HashMap中的每个条目与同一HashMap中的每个条目进行比较? [英] Using Java, how can I compare every entry in HashMap to every other entry in the same HashMap without duplicating comparisons?

查看:103
本文介绍了使用Java,如何在不重复比较的情况下将HashMap中的每个条目与同一HashMap中的每个条目进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用2 for循环来比较所有条目,但我得到重复的比较。因为HashMaps没有排序,所以我无法弄清楚如何消除已经做出的比较。例如,我有类似的东西:

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

与此相关的问题是,第一个条目将与第二个条目和第三个条目进行比较,等等。但是,第二项将再次与第一项相比较,等等。然后第三个条目将与第一个条目,第二个条目,第四个条目等进行比较。是否有更好的方法来迭代HashMaps以避免重复比较?

其他信息:

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

解决方案

如果您不小心,消除重复的成本可能会高于成本至少要对密钥进行冗余比较。



您可以使用 System.identityHashCode(x)


$ b (Map.Entry< Key,Value> entry1:map.entrySet()){
Key key1 = entry1.getKey(); $ b

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

值value2 = entry1.getValue();
//比较value1和value2;
}
}


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() 
            }
        }
     }

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?

Additional information:

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.

You can order the keys using 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;
   }
}

这篇关于使用Java,如何在不重复比较的情况下将HashMap中的每个条目与同一HashMap中的每个条目进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆