需要帮助的一个案件流与分组由我希望能够分组由2个不同的字段,并有其他大小数字段的总和,根据不同的分组。以下是我的实体:
public class Customer {
private String name;
private String type;
private BigDecimal total;
private BigDecimal balance;
// Setter, getter
}
让我们假设我有以下列表作为输入:
Customer custa = new Customer("A", "STANDARD", new BigDecimal("1000"), new BigDecimal("1500"));
Customer custa1 = new Customer("A", "VIP", new BigDecimal("2000"), new BigDecimal("2500"));
Customer custb = new Customer("B", "STANDARD", new BigDecimal("3000"), new BigDecimal("3500"));
Customer custc = new Customer("C", "STANDARD", new BigDecimal("4000"), new BigDecimal("4500"));
Customer custa2 = new Customer("A", "VIP", new BigDecimal("1500"), new BigDecimal("2500"));
List<Customer> listCust = new ArrayList<>();
listCust.add(custa);
listCust.add(custa1);
listCust.add(custb);
listCust.add(custc);
listCust.add(custa2);
[
{"A", "STANDARD", new BigDecimal("1000"), new BigDecimal("1500")},
{"A", "VIP", new BigDecimal("3500"), new BigDecimal("5000")},
{"B", "STANDARD", new BigDecimal("3000"), new BigDecimal("3500")},
{"C", "STANDARD", new BigDecimal("4000"), new BigDecimal("4500")}
]
下面有一个解决方案的开头,但我在添加第二个聚合来总结余额的时候阻止了:
listCust.stream()
.collect(Collectors.groupingBy(Customer::getName
, Collectors.groupingBy(Customer::getType
, Collectors.reducing(BigDecimal.ZERO,Customer::getTotal,BigDecimal::add)))
)
.entrySet()
您可以使用merge函数使用toMap实现这一操作,使用name-type
作为键将客户列表流化并将其收集到Map中,并使用merge函数对具有相同键的客户进行total
和balance
求和
Collection<Customer> result = listCust.stream()
.collect(Collectors.toMap(cu -> cu.getName() + "-" + cu.getType(),
Function.identity(), (c1, c2) -> {
c1.setTotal(c1.getTotal().add(c2.getTotal()));
c1.setBalance(c1.getBalance().add(c2.getBalance()));
return c1;
})).values();
我可以同时使用和吗?
问题内容: 我有一张税单: TaxLine类为 我想基于unique 和合并它们,然后添加预期: 问题答案: 主体与链接问题中的主体相同,只需要一个不同的下游收集器来求和:
问题内容: 我发现的唯一接近的事情是:Elasticsearch中的多个分组方式 基本上,我试图获得与以下查询等效的ES : 年龄和性别本身很容易获得: 这使: 但是现在我需要这样的东西: 请注意,这是针对年龄范围的“映射”,因此它们实际上表示的是:)而不是数字。例如,性别[1](“男性”)细分为[246]的年龄范围[0](“18岁以下”)。 问题答案: 由于您只有2个字段,因此一种简单的方法是使
我有以下文件: 如何发出请求,将Sigle\U 1和Sigle\U 2连接起来,并将值reslut分组? 预期结果示例: 我试过了,但还不完全
问题内容: 如何编写一个将整个字段值而不是单个标记考虑在内的ElasticSearch术语聚合查询?比如,我想通过城市名聚集,但下面的回报,,并作为单独的水桶,不和的水桶预期。 问题答案: 您应该在映射中解决此问题。添加一个not_analyzed字段。如果您还需要分析的版本,则可以创建多字段。 现在在city.raw上创建聚合