Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.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流中,按键对它们进行分组,并找到最大值未按预期工作_Java_Java 8_Java Stream - Fatal编程技术网

从映射的Java流中,按键对它们进行分组,并找到最大值未按预期工作

从映射的Java流中,按键对它们进行分组,并找到最大值未按预期工作,java,java-8,java-stream,Java,Java 8,Java Stream,从输入流m1.getValue().compareTo(m2.getValue())) 收集器。maxBy()生成一个可选的,因为如果maxBy应用于一个空的流,它必须返回一个空的可选的 您可以使用Collectors.toMap而不是Collectors.groupingBy来避免使用可选的s: finalResponse.getChosenStartingMaterials() .stream() .map(Analyte::getMatrixUtilisationMap)

从输入
流m1.getValue().compareTo(m2.getValue()))
收集器。maxBy()
生成一个
可选的
,因为如果
maxBy
应用于一个空的
,它必须返回一个空的
可选的

您可以使用
Collectors.toMap
而不是
Collectors.groupingBy
来避免使用
可选的
s:

finalResponse.getChosenStartingMaterials()
    .stream()
    .map(Analyte::getMatrixUtilisationMap)  // Stream<Map<String,Float>>
    .flatMap(it -> it.entrySet().stream())   
    .collect(Collectors.toMap(Map.Entry::getKey,
                              Map.Entry::getValue,
                              (v1, v2) -> v1.compareTo(v2) >= 0 ? v1 : v2));
finalResponse.getChosenStartingMaterials()
.stream()
.map(分析物::getMatrixUtilisationMap)//流
.flatMap(it->it.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey、,
Map.Entry::getValue,
(v1,v2)->v1.与(v2)>=0?v1:v2)相比;

finalResponse.getChosenStartingMaterials()
.stream()
.map(分析物::getMatrixUtilisationMap)//流
.flatMap(it->it.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey、,
Map.Entry::getValue,
浮动(最大值);

您将获得一个
可选对象。只需执行可选的.get()并收集它?您尝试过在调试器中运行它吗?您的原始代码生成
Map
它是否可以与
.collector(collector.toMap(Map.Entry::getKey,Map.Entry::getValue,Float::max))
finalResponse.getChosenStartingMaterials()
    .stream()
    .map(Analyte::getMatrixUtilisationMap)  // Stream<Map<String,Float>>
    .flatMap(it -> it.entrySet().stream())   
    .collect(Collectors.groupingBy(Map.Entry::getKey, Collectors.maxBy((m1, m2) -> m1.getValue().compareTo(m2.getValue()))))
finalResponse.getChosenStartingMaterials()
    .stream()
    .map(Analyte::getMatrixUtilisationMap)  // Stream<Map<String,Float>>
    .flatMap(it -> it.entrySet().stream())   
    .collect(Collectors.toMap(Map.Entry::getKey,
                              Map.Entry::getValue,
                              (v1, v2) -> v1.compareTo(v2) >= 0 ? v1 : v2));
finalResponse.getChosenStartingMaterials()
    .stream()
    .map(Analyte::getMatrixUtilisationMap)  // Stream<Map<String,Float>>
    .flatMap(it -> it.entrySet().stream())   
    .collect(Collectors.toMap(Map.Entry::getKey,
                              Map.Entry::getValue,
                              Float::max));