Java 8:函数式接口 Supplier:无参数, 有返回值


#Java 8


本文关键词:Java 8, 函数式接口,lambda 。

介绍

全路径: java.util.function.Supplier 。

Java 8 中引入。

源码:

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();
}

示例

代码示例:

import java.util.function.Supplier;

public class Main {

    public static void main(String[] args) {
        Supplier<Long> supplier = () -> {
            return System.currentTimeMillis() + 1;
        };

        System.out.println(supplier.get());
    }

}


( 本文完 )