title: Sum values ​of a property and group them by another property
body: <p>I want to sum the &quot;subtotal&quot; values ​​when they have the same &quot;idarticulo&quot; (they will be grouped by this id). The top 10 best-selling items should return. But the report will not show &quot;idarticulo&quot;, instead there will be the &quot;codigo&quot; property of the entity &quot;Articulo&quot;.</p>
  <pre><code>public class KardArt  {
  
      @EmbeddedId
      private @Getter KardArtId kardArtId;
      private @Getter BigDecimal movimiento;
      private @Getter BigDecimal subtotal;
      private @Getter String cta01;
      private @Getter String idarticulo;
      
      @JsonBackReference
      @ManyToOne(fetch=FetchType.LAZY)
      @JoinColumn(name=&quot;idarticulo&quot;, referencedColumnName=&quot;idarticulo&quot;,insertable=false, 
      updatable=false)
      private @Getter Articulo articulo;
    
  }
  </code></pre>
  <pre><code>public class Articulo {
  
      @Id
      private @Getter String idarticulo;
      private @Getter String desArticu; 
      private @Getter String quiebre1;
      private @Getter String codigo;  
  } 
  </code></pre>
  <p>When trying to solve it I could only bring all the sums. Not the top 10. The query takes 2 seconds. I have had trouble thinking how to assign the value &quot;codigo&quot; instead of &quot;idarticulo</p>
  <pre><code>@RequestMapping(&quot;/listaArtPorSub&quot;)
      public ResponseEntity&lt;Map&lt;String, BigDecimal&gt;&gt; listaArtPorMovTot(@RequestParam(value = &quot;doc1&quot;, required = false, defaultValue = &quot;&quot;) List&lt;String&gt; doc1){
          List&lt;KardArt&gt; listaKar = kardArtService.listaKarPorVentasc(doc1);
  
          List&lt;Articulo&gt; listaArt = articuloService.listaArt();
          Set&lt;String&gt; IdsArticulo = new HashSet&lt;&gt;();
          for (KardArt kardArt : listaKar) {
              IdsArticulo.add(kardArt.getIdarticulo());
          }
  
          Map&lt;String, BigDecimal&gt; salida = new HashMap&lt;String, BigDecimal&gt;();
  
          for (String id : IdsArticulo) {
              BigDecimal total = listaKar.stream().filter(o -&gt; id.equals(o.getIdarticulo())).map(o -&gt; o.getSubtotal()).reduce(BigDecimal.ZERO, BigDecimal::add);
              //con codigo 
              Optional&lt;Articulo&gt; obj = listaArt.stream().filter(o -&gt; id.equals(o.getIdarticulo())).findFirst();
              Articulo articulo = obj.get();      
              salida.put(articulo.getCodigo(), total);
              //salida.put(id, total);
          }
          return ResponseEntity.ok(salida);
      }
  </code></pre>