Java函数式接口

函数式接口

有且只有一个抽象方法的接口被称为函数式接口。

@FunctionalInterface注解

Java 8 为函数式接口引入一个注解 @FunctionalInterface 。该注解用于一个接口的定义上,一旦使用该注解来定义接口,编译器将会强制检查改接口是否确实有且仅有一个抽象方法,否则将会报错。该注解不是必须的,只要符合函数式接口的定义,那改接口就是函数式接口。

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface FunctionalInterface {}

函数式接口格式

修饰符 interface 接口名称{
    //public abstract 可以不写 编译器自动加上
    public abstract 返回值 方法名称(参数列表)
    // 其他方式 
}

常用函数式接口

Function<T,R>: 函数型接口

R apply(T t),传入一个参数,返回想要的结果。

  Function<Integer, String> f1 = f -> String.valueOf(f * 3);
  Function<String, Integer> f2 = f -> Integer.valueOf(f) * 3;

  System.out.println(f1.apply(2));//6
  System.out.println(f2.apply("2"));//6

compose(Function<? super V, ? extends T> before),先执行compose方法参数before中的apply方法,然后将执行结果传递给调用compose函数中的apply方法在执行。

  Function<Integer, String> f1 = f -> String.valueOf(f * 3);
  Function<String, Integer> f2 = f -> Integer.valueOf(f) * 3;

  System.out.println(f1.compose(f2).apply("2"));//18
  System.out.println(f2.compose(f1).apply(2));//18

andThen(Function<? super R, ? extends V> after),先执行调用andThen函数的apply方法,然后在将执行结果传递给andThen方法after参数中的apply方法在执行。它和compose方法整好是相反的执行顺序。

  Function<Integer, String> f1 = f -> String.valueOf(f * 3);
  Function<String, Integer> f2 = f -> Integer.valueOf(f) * 3;

  System.out.println(f1.andThen(f2).apply(2));
  System.out.println(f2.andThen(f1).apply("2"));
  Function<Integer, Integer> f1 = f -> f + 1;
  Function<Integer, Integer> f2 = f -> f * 3;

  System.out.println(f1.compose(f2).apply(2));//7 -> f2.apply -> f1.apply -> 2*3 -> 6+1
  System.out.println(f2.compose(f1).apply(2));//9 -> f1.apply -> f2.apply -> 2+1 -> 3*3

  System.out.println(f1.andThen(f2).apply(2));//9 -> f1.apply -> f2.apply -> 2+1 -> 3*3
  System.out.println(f2.andThen(f1).apply(2));//7 -> f2.apply -> f1.apply -> 2*3 -> 6+1

Consumer:消费型接口

void accept(T t),接收一个参数进行消费,但无需返回结果。

 Consumer<String> c1 = s -> {
    String[] split = s.split(",");
    System.out.println("key:" + split[0] + ",value=" + split[1]);
 };
 c1.accept("k,v");//key:k,value=v

andThen(Consumer<? super T> after),先消费然后在消费,先执行调用andThen接口的accept方法,然后在执行andThen方法参数after中的accept方法。

  Consumer<String> c1 = s -> {
      System.out.println("c1");
  };

  Consumer<String> c2 = s -> {
      System.out.println("c2");
  };

  c1.andThen(c2).accept("1");// c1 -> c2
  c2.andThen(c1).accept("1");// c2 -> c1

Supplier: 供给型接口

T get(),无参数,有返回值。

 Supplier<String> s = () -> "Hello World";

 System.out.println(s.get());//Hello World

Predicate : 断言型接口

boolean test(T t),传入一个参数,返回一个布尔值。

 Predicate<Integer> p1 = p -> p > 0;

 System.out.println(p1.test(1));//true

and(Predicate<? super T> other),相当于逻辑运算符中的&&,当两个Predicate函数的返回结果都为true时才返回true

 Predicate<Integer> p1 = p -> p > 0;
 Predicate<Integer> p2 = p -> p > 1;

 System.out.println(p1.and(p2).test(1));//false
 System.out.println(p1.and(p2).test(2));//true

or(Predicate<? super T> other) ,相当于逻辑运算符中的||,当两个Predicate函数的返回结果有一个为true则返回true,否则返回false

 Predicate<Integer> p1 = p -> p > 0;
 Predicate<Integer> p2 = p -> p > 1;

 System.out.println(p1.or(p2).test(1));//true
 System.out.println(p1.or(p2).test(2));//true

negate() 方法的意思就是取反

 Predicate<Integer> p1 = p -> p > 0;
 Predicate<Integer> p2 = p -> p > 1;

 System.out.println(p1.negate().test(1));//false
 System.out.println(p2.negate().test(2));//false

Bi类型接口

BiConsumerBiFunctionBiPrediateConsumerFunctionPredicate 的扩展,可以传入多个参数,没有 BiSupplier 是因为 Supplier 没有入参。在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值