基于Java8 函数式接口理解及测试
作者:jingxian 发布时间:2021-06-19 21:54:35
标签:java8,函数式接口
1. 函数式接口的理解
根据重构的思想,需要把容易变化的模块进行抽象并封装起来,从这个点来看,Java8新引入的函数式接口就是基于这个思想进行设计的。
2. 函数式接口定义
2.1自定义如下
需要FunctionalInterface关键字显示声明:
@FunctionalInterface
public interface AppleInterface {
public void test();
}
2.2 系统预定义
java.util.function.Consumer;
java.util.function.Function;
java.util.function.Predicate;
java.util.function.Supplier;
可以去查看源码了解具体的细节,这几个接口包括了常用的一些场景,一般可满足需要
3. 函数式接口的使用
函数式接口一般使用前需要先定义,也可以使用系统预定义的几个函数式接口
函数式接口的使用和使用一个变量没有区别,显示声明定义,格式如下:
FunctionInterface interface=null;
这里的interface虽然看起来是一个变量,可是实际却是一段行为代码,用于执行具体的业务逻辑,可以自由在方法接口间传递,也可以直接执行
interface.doSomeThing();
如定义函数式接口为参数的接口:
public void filter(FunctionInterface interface)
{
interface.doSomeThing();
}
4. 函数式接口练习
4.1 自定义实体类Apple
public class Apple {
private String color;
private float weight;
public Apple(String color, float weight) {
this.color = color;
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
}
4.2 自定义函数式接口
该接口有一个test方法,不接收任何参数,也没有任何返回
@FunctionalInterface
public interface AppleInterface {
public void test();
}
4.3 测试自定义函数式接口
@Test
public void DefineFunctionInterface(){
//自定义函数式接口
AppleInterface at=()->System.out.println("define FunctionInterface AppleInterface.");
at.test();
}
至此,就完成一个很简单的函数式接口的定义和调用
4.4 系统预定义函数式接口
Consumer<T>:该接口接收一个对象T,返回void,测试如下
@Test
public void ConsumerTest(){
Consumer<Apple> consumer=(Apple app)->{System.out.println(app.getColor()+","+app.getWeight());};
List<Apple> apps=Arrays.asList(new Apple("red", 120),new Apple("blue", 80),
new Apple("green",100));
ConsumerApple(apps,consumer);
}
public void ConsumerApple(List<Apple> apps,Consumer<Apple> c){
for(Apple app:apps){
c.accept(app);
}
}
Supplier<T>:该接口不接收任何参数,返回一个对象T,测试如下:
@Test
public void SupplierTest(){
Supplier<Apple> supplier=()->{return new Apple("hello supplier",999);};
Apple app=supplier.get();
System.out.println(app.getColor()+","+app.getWeight());
}
Predicate<T>:该接口接收一个对象T,返回一个Boolean
@Test
public void PredicateTest(){
//系统预定义函数式接口测试
Predicate<Apple> p1=(Apple a)->{if(a.getWeight()>90) return true;return false;};
Predicate<Apple> p2=(Apple a)->{if(a.getColor().equals("blue")) return true;return false;};
List<Apple> apps=Arrays.asList(new Apple("red", 120),new Apple("blue", 80),
new Apple("green",100));
filterApple(apps,p1);//筛选重量大于90g的苹果
filterApple(apps,p2);//筛选蓝色的苹果
}
public void filterApple(List<Apple> apps,Predicate<Apple> p){
for(Apple app:apps){
if(p.test(app)){
System.out.println(app.getColor()+","+app.getWeight());
}
}
}
Function<T,R>: 该接口接收一个对象T,经过转换判断,返回一个对象R
@Test
public void FunctionTest(){
Function<String,Apple> function=(String s)->{return new Apple(s,666);};
Apple app=function.apply("red");
System.out.println(app.getColor()+","+app.getWeight());
app=function.apply("green");
System.out.println(app.getColor()+","+app.getWeight());
}
0
投稿
猜你喜欢
- Jedis事务我们使用JDBC连接Mysql的时候,每次执行sql语句之前,都需要开启事务;在MyBatis中,也需要使用openSessi
- 引言对于Nacos大家应该都不太陌生,出身阿里名声在外,能做动态服务发现、配置管理,非常好用的一个工具。然而这样的技术用的人越多面试被问的概
- 1. 异常1.1 try…catch异常处理try catch的异常处理的格式写法 :try{ &nbs
- properties配置文件如下:human.name=Mr.Yuhuman.age=21human.gender=male如何把prope
- 一,内部类访问成员1,内部类可以直接访问外部类的成员,包括私有。2,外部类要访问内部类,必须建立内部类对象。class Outer{int
- 最近一个项目中,需要用到Java的websocket新特性,于是就学了一下,感觉这技术还挺好玩的,瞬间知道网页上面的那些在线客服是怎么做的了
- java 根据经纬度获取地址实现代码实现代码:public class GetLocation { public
- IDEA 报错:无效的源发行版问题描述从SVN拉项目代码到本地后用idea运行,发现几个报错,关键的一个是:无效的源发行版,考虑是JDK版本
- RabbitMQ作为AMQP的代表性产品,在项目中大量使用。结合现在主流的spring boot,极大简化了开发过程中所涉及到的消息通信问题
- java 同步、异步、阻塞和非阻塞分析概要:正常情况下,我们的程序以同步非阻塞的方式在运行。但是我们的程序总会出现一些耗时操作,比如复杂的计
- 现在Web开发越来越倾向于前后端分离,前端使用AngularJS,React,Vue等,部署在NodeJS上,后面采用SpringBoot发
- 方式一public class Test{ public static void main(String[] args) throws Ex
- 相信大家肯定都在电商网站买过东西,当我们看中一件喜欢又想买的东西时,这时候你又不想这么快结账,这时候你就可以放入购物车;就像我们平时去超市买
- 突然心血来潮,想自己做个小程序玩玩,但是怎么把他做成一个exe文件,让大家能够更好的理解和使用呢,百度了一下,说是需要exe4j来生成,但是
- 这篇文章主要介绍了java文件下载代码实例(单文件下载和多文件打包下载),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考
- 我们知道在编程时许多操作(如更新UI)需要在主线程中完成,而且,耗时操作(如网络连接)需要放在子线程中,否则会引起ANR。所以我们常使用Ha
- 一、javaBeanjavaBean:一种类的规格编写规范javaBean在MVC设计模型中是model,又称模型层,在一般的程序中,我们称
- 下截JNative组件jnative.sourceforge.net/ 到这里下载JNative开源项目,我下载的是1.3.2解压JNati
- jdk8之前 一、java.lang.Systemlong times = System.currentTimeMillis();
- 快速排序快速排序是一种比较高效的排序算法,采用“分而治之”的思想,通过多次比较和交换来实现排序,在一