美文网首页
Supplier详解

Supplier详解

作者: felayman | 来源:发表于2017-10-12 13:04 被阅读589次

java.util.function中 Function, Supplier, Consumer, Predicate和其他函数式接口广泛用在支持lambda表达式的API中。这些接口有一个抽象方法,会被lambda表达式的定义所覆盖。

@FunctionalInterface
public interface Supplier<T> {

    T get();
}

顾名思义, Supplier是用来提供一个对象,至于提供的对象的构造则由其来定义.

其核心方法:

  • T get();

    获取提供的对象实例

下面对上述方法进行实例测试:

get()

随机获取一个double类型的值

Supplier <Double>  supplier = () -> Math.random();
 System.out.println(supplier.get());

与Supplier <T>相关的接口

  • BooleanSupplier

    提供一个boolean类型的对象

  • DoubleSupplier

    提供一个double类型的对象

  • IntSupplier

    提供一个int类型的对象

  • LongSupplier

    提供一个long类型的对象

相关文章

网友评论

      本文标题:Supplier详解

      本文链接:https://www.haomeiwen.com/subject/vqenyxtx.html