title: Remove duplicate values from HashMap in Java
body: <p>I have a map with duplicate values:</p>
  
  <pre><code>("A", "1");
  ("B", "2");
  ("C", "2");
  ("D", "3");
  ("E", "3");
  </code></pre>
  
  <p>I would like to the map to have</p>
  
  <pre><code>("A", "1");
  ("B", "2");
  ("D", "3");
  </code></pre>
  
  <p>Do you know how to get rid of the duplicate values?</p>
  
  <p>At present, I get 'java.util.ConcurrentModificationException' error.</p>
  
  <p>Thank you.</p>
  
  <pre><code>public static void main(String[] args) {
  
      HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
      map.put("A", "1");
      map.put("B", "2");
      map.put("C", "2");
      map.put("D", "3");
      map.put("E", "3");
  
      Set&lt;String&gt; keys = map.keySet(); // The set of keys in the map.
  
      Iterator&lt;String&gt; keyIter = keys.iterator();
  
      while (keyIter.hasNext()) {
          String key = keyIter.next();
          String value = map.get(key);
  
          System.out.println(key + "\t" + value);
  
          String nextValue = map.get(key);
  
          if (value.equals(nextValue)) {
              map.remove(key);
          }
      }
      System.out.println(map);
  }
  </code></pre>