title: Using Java, how can I compare every entry in HashMap to every other entry in the same HashMap without duplicating comparisons?
body: <p>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:</p>
  
  <pre><code>    for(Entry&lt;String, String&gt; e1: map.entrySet())
      { 
          for(Entry&lt;String, String&gt; e2: map.entrySet())
          {    
            if (e1.getKey() != e2.getKey())
              {
             //compare e1.getValue() to e2.getValue() 
              }
          }
       }
  </code></pre>
  
  <p>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?  </p>
  
  <p>Additional information:</p>
  
  <p>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.</p>