title: How to simply reduce a collection to an object using Java reduce without the parallel baggage?
body: <p>So I was trying to use <code>reduce</code> to take a <code>List&lt;String&gt;</code> and create a <code>Map&lt;String,List&lt;Integer&gt;&gt;</code> where the key of the map a member of the original <code>List</code>, and the value is a <code>List</code> of indexes where that key occurred.  Generally this is a very simple operation is most other languages using reduce, inject, fold, etc.  For example in Javascript you might just do this:</p>
  
  <pre><code>let mappings = someStrings.reduce( function( mappings, val, index ) {
      if( !mappings[val] ) {
          mappings[ val ] = [];
      }
      mappings[ val ].push( index );
      return mappings;
  }, {});
  </code></pre>
  
  <p>However, what I discovered was reduce in Java was quite complicated because Java takes the view that functional operations should be written so they can be "transparently" parallelized, but that adds extra overhead for simple cases like this.  In Java I have to write something rather painful:</p>
  
  <pre><code>List&lt;String&gt; headers = ...
  Map&lt;String,List&lt;Integer&gt;&gt; mappings = IntStream.range(0, headers.size())
      .map( i -&gt; new Pair&lt;String,Integer&gt;( headers.get(i), i ) )
      .reduce( new HashMap&lt;&gt;(), ( mapping, pair ) -&gt; {
          if( !mappings.contains( pair.getFirst() ) ) {
              mappings.put( pair.getFirst(), new ArrayList&lt;String&gt;() );
          }
          mappings.get( pair.getFirst() ).add( pair.getSecond() );
          return mappings;
      }, ( x, y ) -&gt; x );
  </code></pre>
  
  <p>If I'm forced to actually merge x and y then it gets pretty complex for the simple single thread case when merging things that are complex objects.  That really shows the complexity created from the design decision Java took.</p>
  
  <p>My question is am I missing something about reduce?  Is there a way to reduce (pun intended) the parallel design into single thread design making it simpler that I just don't see?  The option for parallel is nice, but most of the time I'm content with simple single threaded reduce.</p>