Predicate与filter
来源:互联网 发布:windows pptp 代码 编辑:程序博客网 时间:2024/04/29 21:01
怎么根据某些条件来过滤Collection的元素?我们可以在循环里面判断元素是否符合条件然后来remove元素。
Guava已经作了这样的功能。例如你要在String的list里面有"a","b","A",要把非大小写a的元素去掉可以这样:
返回的list2就只有"a"和"A"。要注意的是,原来作为参数传入的list里面还是3个元素,并没有因为被调了filter就被去掉了"b"。另外这个方法作的很好的地方就是返回的list2是带有过滤功能的。也就是说如果你调了list2.add("c")这个方法会抛出IllegalArgumentException,如果调集合的removeAll()和clear()方法也只会对符合predicate条件的元素来操作,这个真的很cool!而且返回的集合不支持remove()。
另外Predicates还提供了很多Predicate子类来实现各种常用功能,例如:
过滤集合中特定的类型:
2个Predicate“与”和“或”:
非null:
用equals来做条件:
- Predicate与filter
- Predicate
- Predicate Information 有2 个取值:filter 和 access
- Java8 新特性之集合: removeIf(Predicate<? super E> filter)
- Java8新特性之集合removeIf(Predicate<? super E> filter)
- 可变参数与Predicate and问题
- Java 8 Stream API features --- filter/reduce & predicate Interface test method
- Delegate,Action,Func,Predicate的使用与区别
- Delegate,Action,Func,Predicate的使用与区别
- Delegate,Action,Func,Predicate的使用与区别
- Delegate,Action,Func,Predicate的使用与区别
- 和Lambda表达式相关的Consumer、Function、Predicate 与 Supplier
- C#中的Predicate<T>与Func<T, bool>
- C#委托之Action、Func与Predicate用法
- bloom filter与Cuckoo Filter
- Predicate使用
- Predicate Information
- Predicate委托
- sp_dropextendedproc
- 关于空项目MFC程序设置问题
- 第一个内核模块Hello Kernel
- 易语言5.11破解精简版
- ubuntu下的一个tar bug
- Predicate与filter
- 禁用 SSH 远程主机的公钥检查
- SRM 503
- C#的自增和自减操作符
- API之路1
- 现在开始做测试了
- AnalogClock与Handler的使用
- ASP防止注入代码:
- Web开发/设计人员应当知道的15个网站