gpt4 book ai didi

基于Java8 函数式接口理解及测试

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章基于Java8 函数式接口理解及测试由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

1. 函数式接口的理解 。

根据重构的思想,需要把容易变化的模块进行抽象并封装起来,从这个点来看,Java8新引入的函数式接口就是基于这个思想进行设计的.

2. 函数式接口定义  。

2.1 自定义如下 。

需要FunctionalInterface关键字显示声明:

?
1
2
3
4
@FunctionalInterface
  public interface AppleInterface {
public void test();
 }

2.2 系统预定义 。

?
1
2
3
4
java.util.function.Consumer;
java.util.function.Function;
java.util.function.Predicate;
java.util.function.Supplier;

可以去查看源码了解具体的细节,这几个接口包括了常用的一些场景,一般可满足需要 。

3. 函数式接口的使用 。

函数式接口一般使用前需要先定义,也可以使用系统预定义的几个函数式接口 。

函数式接口的使用和使用一个变量没有区别,显示声明定义,格式如下:

?
1
FunctionInterface interface=null;

这里的interface虽然看起来是一个变量,可是实际却是一段行为代码,用于执行具体的业务逻辑,可以自由在方法接口间传递,也可以直接执行 。

interface.doSomeThing(),

如定义函数式接口为参数的接口:

?
1
2
3
4
public void filter(FunctionInterface interface)
{
 interface.doSomeThing();
}

4. 函数式接口练习 。

4.1 自定义实体类Apple 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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方法,不接收任何参数,也没有任何返回 。

?
1
2
3
4
@FunctionalInterface
public interface AppleInterface {
   public void test();
}

4.3 测试自定义函数式接口 。

?
1
2
3
4
5
6
@Test
  public void DefineFunctionInterface(){
  //自定义函数式接口
  AppleInterface at=()->System.out.println("define FunctionInterface AppleInterface.");
  at.test();
  }

至此,就完成一个很简单的函数式接口的定义和调用 。

4.4 系统预定义函数式接口 。

Consumer<T>:该接口接收一个对象T,返回void,测试如下 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@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,测试如下:

?
1
2
3
4
5
6
@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 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@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 。

?
1
2
3
4
5
6
7
8
9
@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());
 
  }

以上这篇基于Java8 函数式接口理解及测试就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我.

最后此篇关于基于Java8 函数式接口理解及测试的文章就讲到这里了,如果你想了解更多关于基于Java8 函数式接口理解及测试的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com