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());
}
java8实战:filter的简单使用的更多相关文章
- Java8过滤器(Filter)
1.在Java之前对List进行过滤的方式 public class BeforeJava8 { public static void main(String[] args) { List<Pe ...
- java 调用 C# 类库 实战视频, 非常简单, 通过 云寻觅 javacallcsharp 生成器 一步即可!
java 调用 C# 类库 实战视频, 非常简单, 通过 云寻觅 javacallcsharp 生成器 一步即可! 通过 云寻觅 javacallcsharp 生成器 自动生成java jni类库, ...
- MySQL实战 | 06/07 简单说说MySQL中的锁
原文链接:MySQL实战 | 06/07 简单说说MySQL中的锁 本文思维导图:https://mubu.com/doc/AOa-5t-IsG 锁是计算机协调多个进程或纯线程并发访问某一资源的机制. ...
- 3.2 Lucene实战:一个简单的小程序
在讲解Lucene索引和检索的原理之前,我们先来实战Lucene:一个简单的小程序! 一.索引小程序 首先,new一个java project,名字叫做LuceneIndex. 然后,在project ...
- Java8实战及自己的总结
java8 介绍 java8是2014年3月份,由Oracle发布的一个版本,又称之为jdk1.8,是现再我们在学习和工作中用的最多的一个版本. 在jdk1.8中,java8以添加非常多的新特性, ...
- Java8实战——自己的总结
java8 介绍 java8是2014年3月份,由Oracle发布的一个版本,又称之为jdk1.8,是现再我们在学习和工作中用的最多的一个版本. 在jdk1.8中,java8以添加非常多的新特 ...
- java8实战二------lambda表达式和函数式接口,简单就好
一.Lambda 可以把Lambda表达式理解为简洁地i表示可传递的匿名函数的一种方式:它没有名称,但它有参数列表.函数主体.返回类型,可能还是一个可以抛出的异常列表. 听上去,跟我们用的匿名类,匿名 ...
- Java8实战,
Supplier 1, @FunctionalInterfacepublic interface Supplier<T> { 2, T get(); 3, Supplier<A ...
- Meanshift filter实现简单图片的卡通化效果
利用Meanshift filter和canny边缘检测的效果,可以实现简单的图片的卡通化效果.简单的说,就是用Meanshift filter的结果减去canny算法的结果得到卡通化的效果. ...
随机推荐
- MySQL 5.7 修改root密码
更新 MySQL 5.7 以后通过以下方法无法在修改root密码: ') where user='root'; 查看下MySQL的官方文档发现版本更新后原来user里的password字段已经变更为a ...
- [转]You Could Become an AI Master Before You Know It. Here’s How.
转自:https://www.technologyreview.com/s/608921/ai-algorithms-are-starting-to-teach-ai-algorithms/# You ...
- Ambari安装常见问题
参考自: http://blog.csdn.net/xingxc111/article/details/70667574 http://blog.csdn.net/xfg0218/article/de ...
- 一文说尽 MySQL 优化原理
说起MySQL的查询优化,相信大家收藏了一堆奇技淫巧:不能使用SELECT *.不使用NULL字段.合理创建索引.为字段选择合适的数据类型..... 你是否真的理解这些优化技巧?是否理解其背后的工作原 ...
- DOM confirm setTimeout url刷新
console.log 输出框 alert 弹出框 confirm 确认框 // URL和刷新 location.href 获取URL location.href = "url" ...
- android 逆向
用到两个工具 :dex2jar和jd-gui 1,重命名ContactManager.apk为ContactManager.zip并解压得到文件classes.dex: 2,解压dex2jar-0.0 ...
- 局域网内远程连接OPC配置方法详解
局域网内远程连接OPC配置方法详解 https://wenku.baidu.com/view/20fb8ea6d1d233d4b14e852458fb770bf78a3bcc.html OPC服务 ...
- 黄聪:JQUERY判断操作CHECKBOX选中、取消选中、反选、第二次无法选中的问题
用JQuery做CheckBox全选和反选的时候,遇到一个问题.当用JQ控制全选,全取消一次以后,再次点击全选,发现代码变了,但是CheckBox没有处于选中状态. $("#id" ...
- python的Socket网络编程
计算机网络: 多台独立的计算机用网络通信设备连接起来的网络.实现资源共享和数据传递.比如,我们之前的学过的知识可以将D盘的一个文件传到C盘,但如果你想从你的电脑传一个文件到我的电脑上目前是做不到的; ...
- 【rabbitmq】rabbitmq概念解析--消息确认--示例程序
概述 本示例程序全部来自rabbitmq官方示例程序,rabbitmq-demo: 官方共有6个demo,针对不同的语言(如 C#,Java,Spring-AMQP等),都有不同的示例程序: 本示例程 ...