gpt4 book ai didi

java - 根据字符串长度和属性过滤流

转载 作者:行者123 更新时间:2023-11-30 05:22:06 25 4
gpt4 key购买 nike

最佳答案

您可以首先使用嵌套的groupingBy来确保按属性E分组,并进一步按namelength分组>。按 name 分组时,您可以收集到 TreeMap 中,因为您只对名称的 max 长度的条目感兴趣。这是为了确保您可以进一步将 E 映射到 TreeMap 的 lastEntry 的值。

采用上述方法的解决方案如下所示:

Map<E, List<A>> output = input.stream()
.collect(Collectors.groupingBy(A::getE,
Collectors.groupingBy(e -> e.getName().length(),
TreeMap::new, Collectors.toList()))) // Map<E, TreeMap<Integer, List<A>>> // the nested groupings
.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey,
m -> m.getValue().lastEntry().getValue())); // only max length entry mapped here

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com