就像名字一样,它只是一个供应商,它的任务很简单,给你供货

    Supplier Functional Interface只实现定义了一个方法:get

    1. package java.util.function;
    2. /**
    3. * Represents a supplier of results.
    4. *
    5. * <p>There is no requirement that a new or distinct result be returned each
    6. * time the supplier is invoked.
    7. *
    8. * <p>This is a <a href="package-summary.html">functional interface</a>
    9. * whose functional method is {@link #get()}.
    10. *
    11. * @param <T> the type of results supplied by this supplier
    12. *
    13. * @since 1.8
    14. */
    15. @FunctionalInterface
    16. public interface Supplier<T> {
    17. /**
    18. * Gets a result.
    19. *
    20. * @return a result
    21. */
    22. T get();
    23. }

    就像下面的例子,供应商每次都给你提供一个有一个handsome xiaohundun的Map

    1. package other;
    2. /*
    3. * @author chou
    4. * @Description
    5. * @since 2020/1/13
    6. **/
    7. import org.junit.Test;
    8. import java.util.HashMap;
    9. import java.util.Map;
    10. import java.util.function.Supplier;
    11. public class SupplierTest {
    12. @Test
    13. public void supplierFi () {
    14. // Supplier<Map<Object,Object>> emptyMap = Collections::emptyMap;
    15. // Map<Object, Object> mmmmmmamaipi = emptyMap.get();
    16. // System.out.format("%3$s size %1$x ,hashcode %2$x", mmmmmmamaipi.size(), mmmmmmamaipi.hashCode(), "mmmmmmamaipi");
    17. Supplier<Map<String,String>> handsomeMap = ()->{
    18. Map<String,String> rm = new HashMap<>();
    19. rm.put("xiaohundun", "handsome");
    20. return rm;
    21. };
    22. Map<String,String> handsomeMap1 = handsomeMap.get();
    23. System.out.format("xiaohundun is %s %n", handsomeMap1.get("xiaohundun"));
    24. handsomeMap1.put("xiaohundun", "kind");
    25. Map<String,String> handsomeMap2 = handsomeMap.get();
    26. System.out.format("xiaohundun is %s", handsomeMap2.get("xiaohundun"));
    27. }
    28. }

    Supplier FI在流中的应用:

    IntStream.generate接收一个IntSupplier,此Supplier提供数字,中间peek了一下用来虚区,然后用短路操作终结流。

    1. @Test
    2. public void generate () {
    3. #An infinite stream should be ended by short-circuiting operations such as limit & anyMatch & findFirst etc.
    4. System.out.println(IntStream.generate(() -> (int) (Math.random() * 100)).peek(System.out::println).anyMatch((i) -> i == 6));
    5. }

    Supplier FI在非流中的应用:

    ThreadLocal有一个静态内部类,它集成继承了ThreadLocal,并且内部绑定了Supplier。ThreadLocal提供了withInitial方法用来创建一个包含初始值的ThreadLocal,当你使用ThreadLocal#get方法的时候会检查线程绑定Map是否为空,如果为空进行值的初始化并绑定Thread与ThreadLocalMap,如果不为空返回ThreadLocalMap中的值。

    1. @Test
    2. public void supplierThreadLocal(){
    3. Supplier<String> supplierTheadLocal = () -> "xiaohundun";
    4. ThreadLocal<String> threadLocal = ThreadLocal.withInitial(supplierTheadLocal);
    5. System.out.println(threadLocal.get());
    6. }
    1. @Test
    2. public void Optional () {
    3. OptionalInt value = OptionalInt.empty();
    4. try {
    5. value.orElseThrow(new Supplier<Throwable>() {
    6. @Override
    7. public Throwable get () {
    8. return new ArithmeticException("哈哈");
    9. }
    10. });
    11. } catch (Throwable e) {
    12. System.out.println(e.getMessage());
    13. }
    14. value.orElseGet(new IntSupplier() {
    15. @Override
    16. public int getAsInt () {
    17. return 66;
    18. }
    19. });
    20. }