title: Ignore null in Java stream
body: <p>I am performing some transformations using a Java stream on some data. I would like to ignore any values in a map that contain a null value.</p>
  <p>Example:</p>
  <pre class="lang-java prettyprint-override"><code>HashMap&lt;String, Object&gt; vals = new HashMap&lt;&gt;();
  vals.put(&quot;null&quot;, null);
  vals.put(&quot;string1&quot;, &quot;1 &quot;);
  </code></pre>
  <p>I've tried using filter:</p>
  <pre class="lang-java prettyprint-override"><code>Map&lt;String, Object&gt; nullMap = vals.entrySet().stream()
      .filter(e -&gt; e.getValue() != null)
      .collect(Collectors.toMap(
              e -&gt; e.getKey().trim(),
              e -&gt; (e.getValue() instanceof String ? ((String) e.getValue()).trim() : e.getValue())))
  </code></pre>
  <p>However the returned object has the null key/value pair (in my example &quot;null&quot; -&gt; null) completely removed. How can I retain the key &quot;null&quot; and ignore it during any subsequent processing after the <code>.filter</code> step in the Java stream?</p>
  <p>In the above example I would like to get a Map returned with where the key and value pairs are trimmed and the null value is 'ignored' during the trimming step.</p>