title: How to insert the same value for multiple keys of an HashMap using Java Streams
body: <p>Say I have a HashMap and I want to insert the same value to a list of keys. How can I do this with Java 8 without iterating through all the keys and inserting the value? This is more of a Java Streams question.</p>
  
  <p>Here is the straight forward way of doing it. This is a sample code that I wrote to demonstrate what I wanted to achieve.</p>
  
  <pre><code>public void foo(List&lt;String&gt; keys, Integer value) {
      Map&lt;String, Integer&gt; myMap = new HashMap&lt;&gt;(); 
      for (String key : keys) {
          myMap.put(key, value);
      }
  }
  </code></pre>
  
  <p>Is there a simpler way of doing the above using Java 8 streams? How can I avoid the for loop using Java 8 streams. Thanks!</p>
  
  <p>[Edit-1] A better code snippet below.</p>
  
  <pre><code>public void foo() {
      Map&lt;String, Integer&gt; myMap = new HashMap&lt;&gt;(); 
      List&lt;String&gt; keys = getKeysFromAnotherFunction();
      Integer value = getValueToBeInserted(); // Difficult to show my actual use case. Imagine that some value is getting computed which has to be inserted for the keys.
      for (String key : keys) {
          myMap.put(key, value);
      }  
  
      List&lt;String&gt; keys2 = getNextSetOfKeys();
      Integer newValue = getValueToBeInserted(); 
      for (String key : keys2) {
          myMap.put(key, newValue);
      } 
  }
  </code></pre>