Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java流:转换map';s值_Java_Dictionary_Collections_Java 8_Stream - Fatal编程技术网

Java流:转换map';s值

Java流:转换map';s值,java,dictionary,collections,java-8,stream,Java,Dictionary,Collections,Java 8,Stream,有一个Map的实例,其中B是一个具有属性Integer price(除其他外)的POJO,我如何优雅地将此映射转换为Map,其中Integer表示给定集合的价格之和,对于每个键a?您可以像这样使用流: Map<A, Integer> result = map.entrySet().stream() .collect(Collectors.toMap( Map.Entry::getKey, b ->

有一个
Map
的实例,其中
B
是一个具有属性
Integer price
(除其他外)的POJO,我如何优雅地将此映射转换为
Map
,其中
Integer
表示给定集合的
价格之和,对于每个键
a

您可以像这样使用流:

Map<A, Integer> result = map.entrySet().stream()
        .collect(Collectors.toMap(
                Map.Entry::getKey, 
                b -> b.getValue().stream()
                        .mapToInt(B::getPrice)
                        .sum()
                ));
Map result=Map.entrySet().stream()
.collect(collector.toMap)(
Map.Entry::getKey,
b->b.getValue().stream()
.mapToInt(B::getPrice)
.sum()
));

这应该能帮到你。使用Java 8中的流式API: (在本例中,类“PricePojo”是您的B,“String”是您的A)

Map newMap=Map.entrySet().stream()//
.collect(Collectors.toMap(Entry::getKey,Entry->Entry.getValue().stream().mapToInt(PricePojo::getPrice).sum());
您也可以按照以下示例中的方式实现Map.Entry:

    Map<String, Integer> newMap = map.entrySet().stream()//
    .map(entry -> new Map.Entry<String, Integer>() {
    private int sum = entry.getValue().stream().mapToInt(PricePojo::getPrice).sum();
    @Override
    public String getKey() {
        return entry.getKey();
    }

    @Override
    public Integer getValue() {
        return sum;
    }

    @Override
    public Integer setValue(Integer value) {
        return sum = value.intValue();
    }
    }).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
Map newMap=Map.entrySet().stream()//
.map(条目->新建映射.entry(){
private int sum=entry.getValue().stream().mapToInt(PricePojo::getPrice.sum();
@凌驾
公共字符串getKey(){
return entry.getKey();
}
@凌驾
公共整数getValue(){
回报金额;
}
@凌驾
公共整数设置值(整数值){
返回和=value.intValue();
}
}).collect(Collectors.toMap(条目::getKey,条目::getValue));

@Michael这不是很有帮助的评论。我知道如何用显式循环解决它,但我不确定流是什么。@weno然后展示你用循环的尝试。这样,创建流解决方案非常容易
    Map<String, Integer> newMap = map.entrySet().stream()//
    .map(entry -> new Map.Entry<String, Integer>() {
    private int sum = entry.getValue().stream().mapToInt(PricePojo::getPrice).sum();
    @Override
    public String getKey() {
        return entry.getKey();
    }

    @Override
    public Integer getValue() {
        return sum;
    }

    @Override
    public Integer setValue(Integer value) {
        return sum = value.intValue();
    }
    }).collect(Collectors.toMap(Entry::getKey, Entry::getValue));