title: How to concatenate all map key-values into a string, using streams, with replacements made on each value?
body: <p>I'm a Java 7 person, and have been commanded to never use a for-loop again, with only partial tongue in cheek. I need to take a <code>Map&lt;String,  String&gt;</code>, and end up with a string of command-line parameters, as in the output of the following code (ignoring the "streamy" line for the moment):</p>
  
  <pre><code>forLoop: --name5 "value5"--name4 "value4"--name3 "value3"--name2 "{\"x\": \"y\"}"--name1 "value1"
  resetAllValues: --name5 "value5"--name4 "value4"--name3 "value3"--name2 "{\"x\": \"y\"}"--name1 "value1"
  streamy: --name5 "value5"--name5 "value5"--name4 "value4"--name5 "value5"--name5 "value5"--name4 "value4"--name3 "value3"--name5 "value5"--name5 "value5"--name4 "value4"--name5 "value5"--name5 "value5"--name4 "value4"--name3 "value3"--name2 "{\"x\": \"y\"}"--name5 "value5"--name5 "value5"--name4 "value4"--name5 "value5"--name5 "value5"--name4 "value4"--name3 "value3"--name5 "value5"--name5 "value5"--name4 "value4"--name5 "value5"--name5 "value5"--name4 "value4"--name3 "value3"--name2 "{\"x\": \"y\"}"--name1 "value1"--name5 "value5"--name5 "value5"--name4 "value4"--name5 "value5"--name5 "value5"--name4 "value4"--name3 "value3"--name5 "value5"--name5 "value5"--name4 "value4"--name5 "value5"--name5 "value5"--name4 "value4"--name3 "value3"--name2 "{\"x\": \"y\"}"--name5 "value5"--name5 "value5"--name4 "value4"--name5 "value5"--name5 "value5"--name4 "value4"--name3 "value3"--name5 "value5"--name5 "value5"--name4 "value4"--name5 "value5"--name5 "value5"--name4 "value4"--name3 "value3"--name2 "{\"x\": \"y\"}"--name1 "value1"
  </code></pre>
  
  <p>The names/keys remain unchanged. The values need to be placed in double quotes, and all existing double quotes need to be escaped (<code>\"</code>). The close-quotes <em>should</em> be followed by a space.</p>
  
  <p>I've done it the pre-java-8 way in <code>forLoop</code>, and partially with streams in <code>resetAllValues</code>, which actually alters each entry. The <code>streamy</code> one is way wrong, as it's repeatedly appending <em>all</em> current output onto the next element...</p>
  
  <p>How can this be done efficiently with streams? And how can it be done in a way that doesn't alter the map entries or use a builder? I'm not seeing it yet.</p>
  
  <pre><code>package working;
  
  import java.util.HashMap;
  import java.util.Map;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  import java.util.stream.Collectors;
  
  public class Temp {
     public static void main(String[] cmd_lineParams) {
        Map&lt;String, String&gt; map = new HashMap&lt;&gt;(5);
        map.put("name1", "value1");
        map.put("name2", "{\"x\": \"y\"}");
        map.put("name3", "value3");
        map.put("name4", "value4");
        map.put("name5", "value5");
        forLoop(map);
        resetAllValues(new HashMap&lt;String, String&gt;(map));
        streamy(new HashMap&lt;String, String&gt;(map));
     }
     private static final Matcher matcher = Pattern.compile("\"").matcher("ignored input");
     private static final void forLoop(Map&lt;String, String&gt; map) {
        StringBuilder builder = new StringBuilder();
        for(Map.Entry&lt;String, String&gt; entry : map.entrySet()) {
           String value = matcher.reset(entry.getValue()).replaceAll("\\\\\"");
           builder.append("--").append(entry.getKey()).append(" \"").append(value).append("\"");
        }
        System.out.println("forLoop: " + builder.toString());
     }
  </code></pre>
  
  <p>Continued...</p>
  
  <pre><code>   private static final void resetAllValues(Map&lt;String, String&gt; map) {
        map = map.entrySet().stream()
           .collect(Collectors.toMap(entry -&gt; entry.getKey(),
                                     entry -&gt; matcher.reset(entry.getValue()).replaceAll("\\\\\\\"")));
        StringBuilder builder = new StringBuilder();
        for(Map.Entry&lt;String, String&gt; entry : map.entrySet()) {
           builder.append("--").append(entry.getKey()).append(" \"").append(entry.getValue()).append("\"");
        }
        System.out.println("resetAllValues: " + builder.toString());
     }
     private static final void streamy(Map&lt;String, String&gt; map) {
        StringBuilder builder = new StringBuilder();
        map.forEach((k,v) -&gt; builder.append(
           builder.append("--").append(k).append(" \"").append(
              matcher.reset(v).replaceAll("\\\\\"")).append("\"")));
        System.out.println("streamy: " + builder.toString());
     }
  }
  </code></pre>
  
  <p>(My nine-year-old says I need to say "difficulty" somewhere in this question. So: difficulty.)</p>