java 8开始支持lambda,为了方便 函数式编程 , jdk 在java.util.function包提供了大量可以使用的函数式接口,其中Supplier就是一个常用接口。
一、Supplier
从名字来它是一个数据提供者,只有一个没有入参的get的抽象方法,返回一个T类型的值。
下面是这个接口的源代码:
package java.util.function;
/**
* Represents a supplier of results.
*
* <p>There is no requirement that a new or distinct result be returned each
* time the supplier is invoked.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #get()}.
*
* @param <T> the type of results supplied by this supplier
*
* @since 1.8
*/
@FunctionalInterface
public interface Supplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
下面举例说明:
例子1:通过lambda来定义一个Supplier
private static void testBaseFunction() {
Random random = new Random();
Supplier< Long > randomSupplier = ()->{
return random.nextLong();
};
System.out.println("random long :" + randomSupplier.get());
}
输出:
random long:-2470706042123132983
例子2:通过方法引用来定义一个Supplier
private static void testMethodReference() {
// Person必须包含无参构造函数,否则此处会报错 'Cannot resolve constructor 'Person'
Supplier<Person> personSupplier = Person::new;
Person person = personSupplier.get();
// 通过Person的 默认构造函数 来创建Person实例
person.playWith("Jack Q");
}
public static class Person{
public void playWith(String name){
System.out.println("I like to play with " + name);
}
}
输出:
I like to play with Jack Q
例子3: forEach
ivate static void testForEach() {
List<String> list = Arrays.asList("A","N","C");
list.forEach(System.out::println);
list.stream().forEach(System.out::println);
}
二、支持返回基础数据类型的Supplier
因为java的泛型是不支持 boolean , int, long, double 等基础数据类型的,所以在jdk的java.util.function包下也提供了4个支持返回基础数据类型的Supplier函数式接口。
1. BooleanSupplier
package java.util.function;
/**
* Represents a supplier of {@code boolean}-valued results. This is the
* {@code boolean}-producing primitive specialization of {@link Supplier}.
*
* <p>There is no requirement that a new or distinct result be returned each
* time the supplier is invoked.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #getAsBoolean()}.
*
* @see Supplier
* @since 1.8
*/
@FunctionalInterface
public interface BooleanSupplier {
/**
* Gets a result.
*
* @return a result
*/
boolean getAsBoolean();
}
2. DoubleSupplier
package java.util.function;
/**
* Represents a supplier of {@code double}-valued results. This is the
* {@code double}-producing primitive specialization of {@link Supplier}.
*
* <p>There is no requirement that a distinct result be returned each
* time the supplier is invoked.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #getAsDouble()}.
*
* @see Supplier
* @since 1.8
*/
@FunctionalInterface
public interface DoubleSupplier {
/**
* Gets a result.
*
* @return a result
*/
double getAsDouble();
}
3. LongSupplier
package java.util.function;
/**
* Represents a supplier of {@code long}-valued results. This is the
* {@code long}-producing primitive specialization of {@link Supplier}.
*
* <p>There is no requirement that a distinct result be returned each
* time the supplier is invoked.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #getAsLong()}.
*
* @see Supplier
* @since 1.8
*/
@FunctionalInterface
public interface LongSupplier {
/**
* Gets a result.
*
* @return a result
*/
long getAsLong();
}
4. IntSupplier
package java.util.function;
/**
* Represents a supplier of {@code int}-valued results. This is the
* {@code int}-producing primitive specialization of {@link Supplier}.
*
* <p>There is no requirement that a distinct result be returned each
* time the supplier is invoked.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #getAsInt()}.
*
* @see Supplier
* @since 1.8
*/
@FunctionalInterface
public interface IntSupplier {
/**
* Gets a result.
*
* @return a result
*/
int getAsInt();
}