【无标题】Java四大函数式编程接口

Supplier:供给型函数式接口

Supplier接口:专门用于供给的函数式接口

@FunctionalInterface
public interface Supplier<T> {

    /**
     * Gets a result.
     *
     * @return a result
     */
    T get();
}

专门提供供给功能的接口,使用实例如下:

Supplier<Student> studentSupplier = Student::new;//() -> new Student();//lambda表达式
studentSupplier.get().hello();

Consumer:消费型函数式接口

Consumer接口:专门用于消费某个对象的接口

@FunctionalInterface
public interface Consumer<T> {

    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);

    /**
     * Returns a composed {@code Consumer} that performs, in sequence, this
     * operation followed by the {@code after} operation. If performing either
     * operation throws an exception, it is relayed to the caller of the
     * composed operation.  If performing this operation throws an exception,
     * the {@code after} operation will not be performed.
     *
     * @param after the operation to perform after this operation
     * @return a composed {@code Consumer} that performs in sequence this
     * operation followed by the {@code after} operation
     * @throws NullPointerException if {@code after} is null
     */
    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}

使用实例:传入一个Student对象实例,然后对这个实例进行操作,即为消费

private static final Consumer STUDENT_CONSUMER = student -> System.out.println(student.toString()+"真好吃");
public static void main(String[] args) {
    Supplier studentSupplier = () -> new Student();
    STUDENT_CONSUMER.accept(studentSupplier.get());
}

andThen方法:就是可以在accept()方法之后再进行一些后续行为:

STUDENT_CONSUMER
        .andThen(st -> System.out.println("对吧"))
        .accept(studentSupplier.get());

这个的意思就是执行完accept之后再打印“对吧”

Function函数型函数式接口

这个接口会消费一个对象,然后向外供给另一个对象,相当于前两个的结合体:

Function 函数接口的 andThen() 方法可以让多个 Function 函数连接使用。

其中apply函数和其余两个接口一样

示例:输入一个字符串,获取字符串的长度,然后乘上 2

import java.util.function.Function;

public class Java8FunctionAndThen {
    public static void main(String[] args) {
        Function<String, Integer> lengthFunction = str -> str.length();//消费String,返回Integer
        Function<Integer, Integer> doubleFunction = length -> length * 2;
        Integer doubleLength = lengthFunction.andThen(doubleFunction).apply("www.wdbyte.com");
        System.out.println(doubleLength);
    }
}

如果说andThen的执行顺序是在apply之后,那么compose的执行顺序就是在apply之前:

public static void main(String[] args) {
Function<Integer, Integer> times2 = i -> i*2;
Function<Integer, Integer> squared = i -> i*i;
 
System.out.println(times2.apply(4));
 
System.out.println(squared.apply(4));
 
//32                先4×4然后16×2,先执行apply(4),在times2的apply(16),先执行参数,再执行调用者。
System.out.println(times2.compose(squared).apply(4));  
 
//64               先4×2,然后8×8,先执行times2的函数,在执行squared的函数。
System.out.println(times2.andThen(squared).apply(4));  
 
//16
System.out.println(Function.identity().compose(squared).apply(4));  
}

Predicate:断言式接口

该接口的功能为“真假判断器”:我们实现一个test方法,如果条件为真,那么返回true,反之则返回false

private static final Predicate<Student> STUDENT_PREDICATE = student -> student.score > 60;

上例就是一个判断学生成绩的Predicate断言式接口,其中我们使用lambda表达式实现的就是该接口的test方法

和Function函数式接口相应的,Predicate接口也有其他的方法:

and:且

or:或

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值