title: Use Java 8 streams to transform a Map with nulls
body: <p>I am dealing with a <code>Map&lt;String,String&gt;</code> that has <code>null</code> entries in the key and/or value:</p>
  
  <pre><code>Map&lt;String, String&gt; headers = new HashMap&lt;&gt;();
  headers.put("SomE", "GreETing");
  headers.put("HELLO", null);
  headers.put(null, "WOrLd");
  
  headers.keySet().stream().forEach(k -&gt; System.out.println(k + " =&gt; " + copy.get(k)));
  </code></pre>
  
  <p>I get the following output:</p>
  
  <pre><code>SomE =&gt; GreETing
  HELLO =&gt; null
  null =&gt; WOrLd
  </code></pre>
  
  <p>I need to transform the map, so all the non-null values are converted to lowercase, like so:</p>
  
  <pre><code>some =&gt; greeting
  hello =&gt; null
  null =&gt; world
  </code></pre>
  
  <p>I am trying to use Java 8 streams API, but the following code is throwing <code>NullPointerException</code>:</p>
  
  <pre><code>Map&lt;String,String&gt; copy
      = headers.entrySet()
               .stream()
               .collect(
                   Collectors.toMap(
                       it -&gt; it.getKey() != null ? it.getKey().toLowerCase() : null,
                       it -&gt; it.getValue() != null ? it.getValue().toLowerCase() : null));
  
  copy.keySet().stream().forEach(k -&gt; System.out.println(k + " =&gt; " + copy.get(k)));
  </code></pre>
  
  <p>If I comment out the last two map entries, the program executes, so there must be an issue with how <code>Collectors.toMap</code> works when keys or values are null. How do I use the streams API to work around this?</p>