java8实战:filter的简单使用
《JAVA8实战》中的例子
要实现的功能:通过Apple的color或weight属性,对List<Apple>进行筛选。
1、首先定义com.owl.entity.Apple:
package com.owl.entity; public class Apple { private String color; private Integer weight; public String getColor() { return color; } public void setColor(String color) { this.color = color; } public Integer getWeight() { return weight; } public void setWeight(Integer weight) { this.weight = weight; } }
2、生成一个简单的List<Apple>集合
package com.owl.app; import java.util.ArrayList; import java.util.List; import com.owl.entity.Apple; public class demo { public static void main(String[] args) { List<Apple> appleList = new ArrayList<Apple>(); Apple redApple = new Apple(); redApple.setColor("red"); redApple.setWeight(180); appleList.add(redApple); Apple greenApple = new Apple(); greenApple.setColor("green"); greenApple.setWeight(120); appleList.add(greenApple); } }
3、在com.owl.entity.Apple中定义筛选条件(绿苹果或者重量大于150的苹果)
public static boolean isGreenApple(Apple apple) { return "green".equals(apple.getColor()); } public static boolean isHeavyApple(Apple apple) { return apple.getWeight() > 150; }
4、在com.owl.app.demo中定义接口:
public interface Predicate<T> { boolean test(T t); }
5、在com.owl.app.demo中定义filter方法:
static List<Apple> AppleFilter(List<Apple> apples, Predicate<Apple> p) { List<Apple> resultApples = new ArrayList<Apple>(); for (Apple apple:apples) { if(p.test(apple)) { resultApples.add(apple); } } return resultApples; }
6、在main函数中使用filter筛选苹果(需要定义行为类isGreenApple、isHeavyApple)
List<Apple> greenAppleSet = AppleFilter(appleList, Apple::isGreenApple); List<Apple> heavyAppleSet = AppleFilter(appleList, Apple::isHeavyApple); System.out.println("=======绿苹果======="); for (Apple apple:greenAppleSet) { System.out.println(apple.getColor()); } System.out.println("=======大苹果======="); for (Apple apple:heavyAppleSet) { System.out.println(apple.getWeight()); }
结果:
为了实现上述功能,除了需要定义筛选条件之外,仍需要定义Predicate<T>和AppleFilter方法未免太过麻烦,通过lambda表达式有更简单的写法:
List<Apple> greenAppleSet = appleList.stream().filter((Apple apple)->apple.getColor().equals("green")).collect(Collectors.toList());
List<Apple> heavyAppleSet = appleList.stream().filter((Apple apple)->apple.getWeight()>150).collect(Collectors.toList());
System.out.println("=======绿苹果=======");
for (Apple apple:greenAppleSet) {
System.out.println(apple.getColor());
}
System.out.println("=======大苹果=======");
for (Apple apple:heavyAppleSet) {
System.out.println(apple.getWeight());
}
涉及到较大的数据集的时候,可以采用并行处理的方式进行筛选:
List<Apple> greenAppleSet = appleList.parallelStream().filter((Apple apple)->apple.getColor().equals("green")).collect(Collectors.toList()); List<Apple> heavyAppleSet = appleList.parallelStream().filter((Apple apple)->apple.getWeight()>).collect(Collectors.toList());
或者使用匿名函数的形式:
List<Apple> greenAppleSet = AppleFilter(appleList, new Predicate<Apple>() { public boolean test(Apple apple) { return "green".equals(apple.getColor()); }; }); List<Apple> heavyAppleSet = AppleFilter(appleList, new Predicate<Apple>() { public boolean test(Apple apple) { return apple.getWeight() > 150; }; }); System.out.println("=======绿苹果======="); for (Apple apple:greenAppleSet) { System.out.println(apple.getColor()); } System.out.println("=======大苹果======="); for (Apple apple:heavyAppleSet) { System.out.println(apple.getWeight()); }
最新文章
- Activity的Launch Mode
- Spring3 整合 Hibernate4实现数据库操作(1)
- 我的新发现:AVL树旋转的一个特性
- SSH+DWZ、JQuery-UI ,swfobject.embedSWF属性与用法,IE下日期控件被flash控件挡住
- Shell’s Sort
- java数据结构之列表——ArrayList,LinkedList,比较
- 如何用C代码生成二维码
- 写过的HTML标签(一)
- GUI编程笔记(java)05:GUI事件监听机制原理和举例说明
- CATransform3D参数的意义
- Java基础——深入理解Java中的final关键字(转载)
- trimpath javascript的学习
- sass入门学习篇(二)
- 编译原理-NFA构造DFA
- html块、含样式的标签
- linux 系统filezilla无法上传文件 553 Could not create
- 使用正则表达式来截取nginx中的内置变量
- JAVA代码保护从入门到放弃
- 转:php防止sql注入的一点心得
- Android 基于TCP多线程通信实现群聊天的功能
热门文章
- create-react-app 搭建的项目中,使用 stylus
- spark机器学习
- JVM 新生代与老年代
- Laravel + go-micro + grpc 实践基于 Zipkin 的分布式链路追踪系统 摘自https://mp.weixin.qq.com/s/JkLMNabnYbod-b4syMB3Hw?
- Xshell配置ssh使用密钥公钥(publice key)登录
- c# .net WebRequest 始终报域名无法解析
- WPF Demo13 GridSplitter
- STL基础--流
- Javascript中变量提升的问题(五)
- Android开发之Activity(cho1)篇