title: Java 8 stream map on entry set
body: <p>I'm trying to perform a map operation on each entry in a <code>Map</code> object.</p>
  
  <p>I need to take a prefix off the key and convert the value from one type to another. My code is taking configuration entries from a <code>Map&lt;String, String&gt;</code> and converting to a <code>Map&lt;String, AttributeType&gt;</code> (<code>AttributeType</code> is just a class holding some information. Further explanation is not relevant for this question.)</p>
  
  <p>The best I have been able to come up with using the Java 8 Streams is the following:</p>
  
  <pre><code>private Map&lt;String, AttributeType&gt; mapConfig(Map&lt;String, String&gt; input, String prefix) {
     int subLength = prefix.length();
     return input.entrySet().stream().flatMap((Map.Entry&lt;String, Object&gt; e) -&gt; {
        HashMap&lt;String, AttributeType&gt; r = new HashMap&lt;&gt;();
        r.put(e.getKey().substring(subLength), AttributeType.GetByName(e.getValue()));
        return r.entrySet().stream();
     }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
  }
  </code></pre>
  
  <p>Being unable to construct an <code>Map.Entry</code> due to it being an interface causes the creation of the single entry <code>Map</code> instance and the use of <code>flatMap()</code>, which seems ugly.</p>
  
  <p>Is there a better alternative? It seems nicer to do this using a for loop:</p>
  
  <pre><code>private Map&lt;String, AttributeType&gt; mapConfig(Map&lt;String, String&gt; input, String prefix) {
     Map&lt;String, AttributeType&gt; result = new HashMap&lt;&gt;(); 
     int subLength = prefix.length(); 
     for(Map.Entry&lt;String, String&gt; entry : input.entrySet()) {
        result.put(entry.getKey().substring(subLength), AttributeType.GetByName( entry.getValue()));
     }
     return result;
  }
  </code></pre>
  
  <p>Should I avoid the Stream API for this? Or is there a nicer way I have missed?</p>