title: java - How to remove all null elements from a Map?
body: <p>The simplest form of question is I have a collection and I want to delete/remove all elements that their <code>value == null</code>. I don't want delete in place (I want to get new Map so my original Map is safe).</p>
  
  <pre><code>public Map&lt;String, String&gt; ADDITIONAL_QUERY = new HashMap&lt;&gt;();
  this.ADDITIONAL_QUERY.put("deviceManufacturer", payload.getManufacturer());
  this.ADDITIONAL_QUERY.put("deviceModel", payload.getModel());
  this.ADDITIONAL_QUERY.put("source", payload.getSource());
  this.ADDITIONAL_QUERY.put("adrIMEI", payload.getIMEI());
  this.ADDITIONAL_QUERY.put("adrMEID", payload.getMEID());
  this.ADDITIONAL_QUERY.put("adrUDID", payload.getUuid() == null ? null : payload.getUuid().toString());
  this.ADDITIONAL_QUERY.put("adrID", payload.getAndroidId());
  this.ADDITIONAL_QUERY.put("adrSERIAL", payload.getSerial());
  </code></pre>
  
  <p>I add more elements to ADDITIONAL_QUERY depends of what API I'm calling later.</p>
  
  <h2>More details</h2>
  
  <p>I just upgraded my Retrofit lib to version 2.0. I have <code>@FieldMap Map&lt;String, String&gt; additionalQuery</code> in majority of my APIs (around 100 APIs). According to their doc regard <code>FieldMap</code>,"Named key/value pairs for a form-encoded request. A null value for the map, as a key, or as a value is not allowed.", as mentioned on java doc (It's so funny that their doc on web says you can <a href="https://square.github.io/retrofit/2.x/retrofit/index.html?retrofit2/http/class-use/FieldMap.html" rel="nofollow noreferrer">https://square.github.io/retrofit/2.x/retrofit/index.html?retrofit2/http/class-use/FieldMap.html</a>).</p>
  
  <p><a href="https://i.stack.imgur.com/nMVyO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nMVyO.png" alt="enter image description here"></a></p>
  
  <p>As APIs are getting called a lot, I'm looking for a way with minor overhead and fast of course.</p>