Java 如何从另一个地图中的地图获取统计信息

Java 如何从另一个地图中的地图获取统计信息,java,java-stream,Java,Java Stream,我需要找到为特定类别支付最多的客户。 信息就在这张购物地图里面。 产品有价格,长就是产品的数量。 对于Finish,我想要一张看起来像那张地图的地图 一开始我写了两种方法 enter code here public Map<Customer, Map<Category, BigDecimal>> whoPaydMostPerCategory() { Map<Customer, Map<Category, BigDecimal>&

我需要找到为特定类别支付最多的客户。 信息就在这张购物地图里面。 产品有价格,长就是产品的数量。 对于Finish,我想要一张看起来像那张地图的地图

一开始我写了两种方法

enter code here
   public Map<Customer, Map<Category, BigDecimal>> whoPaydMostPerCategory() {

        Map<Customer, Map<Category, BigDecimal>> collect = shopping.entrySet().stream()
                .collect(Collectors.toMap(x -> x.getKey(), y -> pricePerCategory(y.getValue())));

        return collect;
    }

    public Map<Category, BigDecimal> pricePerCategory(Map<Product, Long> map) {

        return  map.entrySet().stream()
                .collect(Collectors.toMap(x -> x.getKey().getCategory(),
                        y -> y.getKey().getPrice().multiply(BigDecimal.valueOf(y.getValue())), (a,b)-> a.add(b)));
    }
在此处输入代码
公共地图whoPaydMostPerCategory(){
Map collect=shopping.entrySet().stream()
.collect(Collectors.toMap(x->x.getKey(),y->pricePerCategory(y.getValue()));
回款到付;
}
公共地图价格分类(地图地图){
返回map.entrySet().stream()
.collect(Collectors.toMap(x->x.getKey().getCategory(),
y->y.getKey().getPrice().multiply(BigDecimal.valueOf(y.getValue()),(a,b)->a.add(b));
}

所以下一步是什么?

首先,与客户一起展平内部地图条目,以创建
[客户,类别,BigDecimal]
列表。然后按
price
反向排序,并使用
Collectors.toMap
作为
的地图进行收集,其中使用合并功能获取第一个
客户

whoPaydMostPerCategory()
    .entrySet()
    .stream()
    .flatMap(e -> e.getValue()
                   .entrySet()
                   .stream()
                   .map(ee -> Map.entry(e.getKey(), ee)))
    .sorted(Comparator.comparing(e -> e.getValue().getValue().negate()))
    .collect(Collectors.toMap(x -> x.getValue().getKey(), y -> y.getKey(), (a,b) -> a));

首先,与客户一起展平内部地图条目,以创建
[客户,类别,BigDecimal]
列表。然后按
price
反向排序,并使用
Collectors.toMap
作为
的地图进行收集,其中使用合并功能获取第一个
客户

whoPaydMostPerCategory()
    .entrySet()
    .stream()
    .flatMap(e -> e.getValue()
                   .entrySet()
                   .stream()
                   .map(ee -> Map.entry(e.getKey(), ee)))
    .sorted(Comparator.comparing(e -> e.getValue().getValue().negate()))
    .collect(Collectors.toMap(x -> x.getValue().getKey(), y -> y.getKey(), (a,b) -> a));

按值排序,获取第一个条目,获取类别,反转mapI无法按值排序,因为in category不是唯一的一个。例如,我们可以有AGD、RTV、食品等类别。我需要为所有不同类别分别支付最多费用的信息。
BigDecimal
s是用于确定最昂贵类别的值,并筛选出restsort by值,获取第一个条目,获取类别,反转mapI无法按值排序的值,因为in category不是唯一的类别。例如,我们可以有AGD、RTV、食品等类别。我需要信息谁为所有不同的类别分别支付最多。
BigDecimal
s是用来确定最昂贵的一个并过滤掉其余部分的值