从映射的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));