title: Most convenient idiom to concatenate string values by key in Java
body: <p>Which is the most convenient way to concatenate string values in a map in Java 8 and 9. I have currently this code in a project:</p>
  
  <pre><code>private static void addToMap2(Map&lt;String, ? super CharSequence&gt; invalidMap, String key, String newMessage) {
      if (invalidMap.containsKey(key)) {
          invalidMap.put(key, invalidMap.get(key) + " and " + newMessage);
      } else {
          invalidMap.put(key, newMessage);
      }
  }
  </code></pre>
  
  <p>Is this code really equivalent to:</p>
  
  <pre><code>private static void addToMap3(Map&lt;String, ? super CharSequence&gt; invalidMap, String key, String value) {
      invalidMap.merge(key, value, (s, s2) -&gt; s + " and " + s2);
  }
  </code></pre>
  
  <p>?</p>
  
  <p>Are there any other better alternatives?</p>