gpt4 book ai didi

java - 在 Java 8 列表上过滤

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

我使用 fj.data.List 提供的 List 类型在函数式 java 中有一个 List 类型的列表

import fj.data.List

List<Long> managedCustomers

我正在尝试使用以下方法对其进行过滤:

managedCustomers.filter(customerId -> customerId == 5424164219L)

我收到这条消息

enter image description here

根据文档,List 有一个 filter 方法,这应该可以工作 http://www.functionaljava.org/examples-java8.html

我错过了什么?

谢谢

最佳答案

正如@Alexis C 在评论中指出的那样

managedCustomers.removeIf(customerId -> customerId != 5424164219L);

如果 customerId 等于 5424164219L,应该会为您提供筛选列表。


编辑 - 上面的代码修改了现有的 managedCustomers,删除了其他条目。另一种方法是使用 stream().filter() 作为 -

managedCustomers.stream().filter(mc -> mc == 5424164219L).forEach(//do some action thee after);

编辑 2 -

对于具体的fj.List,可以使用-

managedCustomers.toStream().filter(mc -> mc == 5424164219L).forEach(// your action);

关于java - 在 Java 8 列表上过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42039845/

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