gpt4 book ai didi

java - 如何处理对象未能满足java 8流中的过滤器

转载 作者:搜寻专家 更新时间:2023-11-01 01:15:34 25 4
gpt4 key购买 nike

我正在尝试处理不满足流中过滤条件的对象。

List<Integer> list = Arrays.asList(1,23,43,12,4,5);
list.stream().filter( i -> i > 10).collect(Collections.toList);

这将返回一个大于 10 的对象列表。但我也想处理无法满足条件 (>10) 的对象。

谢谢。

最佳答案

Map<Boolean, List<Integer>> map = list.stream()
.collect(Collectors.partitioningBy(i > 10));


map.get(false/true).... do whatever you want with those that failed or not

关于java - 如何处理对象未能满足java 8流中的过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52057413/

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