例如:
Supplier<String> supplier = () -> "Hello World!";
String result = supplier.get();
System.out.println(result);
例如:
String str = "Hello World!";
Supplier<String> supplier = str::toUpperCase;
String result = supplier.get();
System.out.println(result);
例如:
Optional<String> optional = Optional.ofNullable(null);
Supplier<String> supplier = () -> optional.orElse("No value");
String result = supplier.get();
System.out.println(result);
例如:
Supplier<Integer> supplier = () -> new Random().nextInt(100);
Stream.generate(supplier)
.limit(10)
.forEach(System.out::println);
例如:
Supplier<Integer> supplier = () -> {
try {
return Integer.parseInt("abc");
} catch (NumberFormatException e) {
return 0;
}
};
Integer result = supplier.get();
System.out.println(result);