java8四大核心内置函数式接口

                 我们在使用lambda表达式的时候,通常需要定义一个接口,如果我们每次都去定义一个接口,显然是有点儿麻烦的,

所以java8中提供了四个内置的函数式接口,通过直接使用这四个接口,或者使用它们的扩展接口,就可以让我们很方便的使用lambda表达式。

               1.Consumer(消费型接口)接收一个参数,无返回值

                

@FunctionalInterface
public interface Consumer<T> {

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

 

            2.Supplier(提供型接口) 无参,有返回值

@FunctionalInterface
public interface Supplier<T> {

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

 

         3.Function(函数接口)接收一个参数,有返回值

@FunctionalInterface
public interface Function<T, R> {

    /**
     * Applies this function to the given argument.
     *
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);
}

 

        4.Predicat(断言型接口)接收一个参数,返回boolean

@FunctionalInterface
public interface Predicate<T> {

    /**
     * Evaluates this predicate on the given argument.
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(T t);
}

   上述四个函数式接口在stream api中被大量的使用,理解好这四个接口对我们熟练的使用stream api很有好处。

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值