title: How to check if a Map has duplicate key?
body: <p>Ok so here's what I have to do:</p>
  
  <p>Split the passed <code>List</code> into individual lines, and then split those lines using a delimiter and then add those parts to a <code>Map</code>.</p>
  
  <p>My code:</p>
  
  <pre><code>public GrammarSolver(List&lt;String&gt; rules) {
      if(rules == null || rules.size() == 0) {
          throw new IllegalArgumentException();
      }
      Map&lt;String, String&gt; rulesMap = new HashMap&lt;String, String&gt;();
      Iterator&lt;String&gt; i = rules.iterator();
      while(i.hasNext()) {
          String rule = i.next(); // store a line from 'rules' List
          String[] parts = rule.split("::="); // split the line into non-terminal and terminal
          rulesMap.put(parts[0], parts[1]); // Put the two parts into the map
      }
      // TODO: exception when duplicate key in map
  }
  </code></pre>
  
  <p>Everything works fine, but now my assignment says that I need to throw an exception if the key of any line is duplicated (occurring more than once).</p>
  
  <p>From what I understand, keys can only be unique, so what am I missing here? </p>