title: java stream pipeline to create map of lists
body: <p>Its not real code, but an analogy to my exact scenario. Main concern here is the util stream pipeline.</p>
  <p>Lets assume, I've following object model</p>
  <pre><code>    @Data
      class ItemRequest {
         String action;
         List&lt;ItemId&gt; itemIds;
      }
      @Data
      class ItemId{
         ItemType itemType;
         long itemKey;
      }
      @Getter 
      @AllArgsConstructor
      enum ItemType{
  
        String backingService;
        String description; 
  
        ITEM_TYPE_1(&quot;backingService1&quot;,&quot;type 1 description&quot;),
        ITEM_TYPE_2(&quot;backingService1&quot;,&quot;type 2 description&quot;),
        ITEM_TYPE_3(&quot;backingService2&quot;,&quot;type 3 description&quot;),
        ITEM_TYPE_4(&quot;backingService2&quot;,&quot;type 4 description&quot;),
        ITEM_TYPE_5(&quot;backingService2&quot;,&quot;type 5 description&quot;),
        ITEM_TYPE_6(&quot;backingService3&quot;,&quot;type 6 description&quot;),
        ITEM_TYPE_7(&quot;backingService3&quot;,&quot;type 7 description&quot;),
       //   so on
      }
  </code></pre>
  <p>Now each ItemType has a different backend microservice.
  My ItemType enum has getters to return backing service.
  So I want to break down my ItemRequest by backing service.</p>
  <p>I can easily do it in imperative way or running 2 stream pipeline.
  But I want to use it in one stream pipeline</p>
  <p><strong>For Example, in simple terms, my question is
  How to combine following 2 steps I wrote  below into one pipeline</strong>
  .</p>
  <pre><code>Map&lt;String,ItemRequest&gt; breakItemRequestAsPerBackingService
   (ItemRequest originalItemRequest ){
      Map&lt;String, List&lt;ItemId&gt;&gt; collect
       = originalItemRequest
                 .getItemIds()
                 .stream()
                 .collect(Collectors.groupingBy(
                       e -&gt; e.getItemType().getBackingService()));
  
  
  
      return collect 
      .entrySet()
      .stream()
      .collect(toMap(
               Map.Entry::getKey,
                e -&gt; new ItemRequest(
                       originalItemRequest.getAction(), 
                       e.getValue())));
  }
  </code></pre>