title: How to form a list of items inside the stream?
body: <p>From my database bellow:</p>
  <pre><code>CD_STORE| CD_PRODUCT | LAST_PURCHASE       |
  4077    | 63920      | 2018-12-05 00:00:00 |
  7003    | 64520      | 2019-03-13 17:55:40 |
  7003    | 76008      | 2013-06-22 00:00:00 |
  </code></pre>
  <p>I want to do a query to return an output where for each store code, I have a product list containing the product code and the last purchase date:</p>
  <pre><code>[
      {
          &quot;storeCode&quot;: 7003,
          &quot;products&quot;: [
              { 
                  &quot;productCode&quot;: 64520,
                  &quot;lastPurchase&quot;: &quot;2020-08-06&quot;
              },
              {
                  &quot;productCode&quot;: 76008,
                  &quot;lastPurchase&quot;: &quot;2018-12-21&quot;
              }
          ]
      },
      {
          &quot;storeCode&quot;: 4077,
          &quot;products&quot;: [
              {
                  &quot;productCode&quot;: 63920,
                  &quot;lastPurchase&quot;: &quot;2021-03-10&quot;
              }
          ]
      }
  ]
  </code></pre>
  <p>Note that in json above, products from the same store (in this case storeCode 7003) are within the same object. But my code is producing different output, each product is being represented as a separate line instead of being grouped within the same store code the way I want:</p>
  <pre><code>[
      {
          &quot;storeCode&quot;: 7003,
          &quot;products&quot;: [
              { 
                  &quot;productCode&quot;: 64520,
                  &quot;lastPurchase&quot;: &quot;2020-08-06&quot;
              }
          ]
      },
      {
          &quot;storeCode&quot;: 7003,
          &quot;products&quot;: [
              {
                  &quot;productCode&quot;: 76008,
                  &quot;lastPurchase&quot;: &quot;2018-12-21&quot;
              }
          ]
      },
      {
          &quot;storeCode&quot;: 4077,
          &quot;products&quot;: [
              {
                  &quot;productCode&quot;: 63920,
                  &quot;lastPurchase&quot;: &quot;2021-03-10&quot;
              }
          ]
      }
  ]
  </code></pre>
  <p>Below is my code that produced the previous output. What is wrong? Why is this happening?</p>
  <p>My classes:</p>
  <pre><code>public class StorePurchaseView  {
  
      private Integer storeCode;
  
      private Set&lt;StorePurchaseItemsView&gt; products;
  
  }
  
  
  public class StorePurchaseItemsView {
  
      private Long productCode;
  
      private LocalDate lastPurchase;
  }
  </code></pre>
  <p>My converter:</p>
  <pre><code>public Set&lt;StorePurchaseView&gt; viewConvert(Collection&lt;Tuple&gt; list) {
  
          return list
                  .stream()
                  .map(tuple -&gt; StorePurchaseView.builder()
                          .storeCode(ofNullable(tuple.get(&quot;CD_STORE&quot;)).isPresent()
                                  ? Integer.parseInt(tuple.get(&quot;CD_STORE&quot;).toString())
                                  : null)
                          .products(Set.of(StorePurchaseItemsView
                                  .builder()
                                  .productCode(ofNullable(tuple.get(&quot;CD_PRODUCT&quot;)).isPresent()
                                          ? Long.parseLong(tuple.get(&quot;CD_PRODUCT&quot;).toString())
                                          : null)
                                  .lastPurchase(buildLastPurchase(tuple))
                                  .build()))
                          .build())
                  .collect(Collectors.toSet());
  
      }
  </code></pre>
  <p><strong>#Edit 1</strong> -&gt; Following code brings up a compilation error <code>Type mismatch: cannot convert from Map&lt;Object,Set&lt;Object&gt;&gt; to Map&lt;Integer,Set&lt;StoreRepurchaseItemsView&gt;&gt;</code></p>
  <pre><code> Map&lt;Integer, Set&lt;StoreRepurchaseItemsView&gt;&gt; itemsViewMap = list.stream()
                  .collect(Collectors.groupingBy(item -&gt; Integer.parseInt(item.get(&quot;CD_STORE&quot;).toString()),
                          Collectors.mapping(item -&gt; Long.parseLong(item.get(&quot;CD_PRODUCT&quot;).toString()),
                                  Collectors.toSet())));
  
          itemsViewMap.entrySet().stream()
                  .map(e -&gt; StoreRepurchaseView.builder()
                          .storeCode(e.getKey())
                          .products(e.getValue())
                          .build())
                  .collect(Collectors.toList());
  </code></pre>