title: How to remove Keys that would cause Collisions before executing Collectors.toMap()
body: <p><a href="https://stackoverflow.com/questions/32312876/ignore-duplicates-when-producing-map-using-streams">I have a stream of objects similar to this previous question</a>, however, instead of ignoring duplicate values, I would like to remove any values from that stream beforehand and print them out.</p>
  <p>For example, from this snippet:</p>
  <pre><code>Map&lt;String, String&gt; phoneBook = people.stream()
                                        .collect(toMap(Person::getName,
                                                       Person::getAddress));
  </code></pre>
  <p>If there were duplicate entries, it would cause a <code>java.lang.IllegalStateException: Duplicate key</code> error to be thrown.</p>
  <p>The solution proposed in that question used a <code>mergeFunction</code> to keep the first entry if a collision was found.</p>
  <pre><code>Map&lt;String, String&gt; phoneBook = 
      people.stream()
            .collect(Collectors.toMap(
               Person::getName,
               Person::getAddress,
               (address1, address2) -&gt; {
                   System.out.println(&quot;duplicate key found!&quot;);
                   return address1;
               }
            ));
  </code></pre>
  <p>Instead of keeping the first entry, if there is a collision from a duplicate key in the stream, I want to know which value caused the collision and make sure that there are no occurrences of that value within the resulting map.</p>
  <p>I.e. if <code>&quot;Bob&quot;</code> appeared three times in the stream, it should not be in the <em>map</em> even once.</p>
  <p>In the process of creating that map, I would like to filter out any duplicate names and record them some way.</p>
  <p>I want to make sure that when creating the list there can be no duplicate entry and for there to be some way to know which entries had duplicate keys in incoming stream. I was thinking about using <code>groupingBy</code> and <code>filter</code> beforehand to find the duplicate keys, but I am not sure what the best way to do it is.</p>