Nir*_*cov 4 java functional-programming functional-java java-8
我使用 fj.data.List 提供的 List 类型在功能 Java 中有一个 List 类型列表
import fj.data.List
List<Long> managedCustomers
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用以下方法对其进行过滤:
managedCustomers.filter(customerId -> customerId == 5424164219L)
Run Code Online (Sandbox Code Playgroud)
我收到这条消息
根据文档,List 有一个过滤器方法,这应该可以工作 http://www.functionaljava.org/examples-java8.html
我错过了什么?
谢谢
正如@Alexis C 在评论中已经指出的那样
managedCustomers.removeIf(customerId -> customerId != 5424164219L);
Run Code Online (Sandbox Code Playgroud)
如果customerId等于,应该为您提供过滤列表5424164219L。
编辑- 上面的代码修改现有的managedCustomers删除其他条目。还有另一种方法是使用stream().filter()as -
managedCustomers.stream().filter(mc -> mc == 5424164219L).forEach(//do some action thee after);
Run Code Online (Sandbox Code Playgroud)
编辑 2 -
具体来说fj.List,您可以使用 -
managedCustomers.toStream().filter(mc -> mc == 5424164219L).forEach(// your action);
Run Code Online (Sandbox Code Playgroud)