Functional interfaces:
Interface | Method | Return type |
---|---|---|
Supplier<T> |
get() |
T |
Consumer<T> |
accept(T) |
void |
<BiConsumer<T, U> |
accept(T, U) |
T |
Predicate<T> |
test(T) |
boolean |
BiPreidcate<T, U> |
test(T, U) |
boolean |
Function<T, R> |
apply(T) |
R |
BiFunction<T, U, R> |
apply(T, U) |
R |
UnaryOperator<T> |
apply(T) |
T |
BinaryOperator<T> |
apply(T, T) |
T |
For setting development for Java 17, see https://stackoverflow.com/questions/70083274/java-17-java-invalid-source-release-7-with-enable-preview-preview-language/70083285#70083285
Use Supplier
packageorg.example;importjava.time.LocalDate;importjava.util.function.Supplier;publicclassApp{publicstaticvoidmain(String[] args){Supplier<LocalDate> localDateSupplier =LocalDate::now;Supplier<LocalDate> localDateSupplier1 =()->LocalDate.now();LocalDate localDate = localDateSupplier.get();LocalDate localDate1 = localDateSupplier1.get();System.out.print("localDate = ");System.out.println(localDate);System.out.print("localDate1 = ");System.out.println(localDate1);}}
result
localDate =2021-11-23
localDate1 =2021-11-23
More complete object
ArrayList<String> stringArrayList =newArrayList<>();
stringArrayList.add("Do Nhu Vy");
stringArrayList.add("Nguyen Bich Van");
stringArrayList.add("Nguyen Hong Do");
stringArrayList.add("Nguyen Tien Nam");Supplier<ArrayList<String>> arrayListSupplier2 =()->{return stringArrayList;};ArrayList<String> strings2 = arrayListSupplier2.get();System.out.print("strings2 = ");System.out.println(strings2);
or other way
Supplier<ArrayList<String>> arrayListSupplier2 =()->{ArrayList<String> stringArrayList =newArrayList<>();
stringArrayList.add("Do Nhu Vy");
stringArrayList.add("Nguyen Bich Van");
stringArrayList.add("Nguyen Hong Do");
stringArrayList.add("Nguyen Tien Nam");return stringArrayList;};ArrayList<String> strings2 = arrayListSupplier2.get();System.out.print("strings2 = ");System.out.println(strings2);
result
strings =[]
strings2 =[Do Nhu Vy, Nguyen Bich Van, Nguyen Hong Do, Nguyen Tien Nam]
How Java virtual machine manage object what is instance of Supplier
interface? Try an experiment:
System.out.print("arrayListSupplier2 = ");System.out.println(arrayListSupplier2);
result
arrayListSupplier2 = org.example.App$$Lambda$19/[email protected]
Implicity way, this is the result of arrayListSupplier2.toString()
. Symbol $$
means object exist on memory only, not in hard disk. Exactly, arrayListSupplier2
initialized by a lambda expression as you seen in the below.
Implement Consumer
interface
packageorg.example;importjava.util.HashMap;importjava.util.function.BiConsumer;importjava.util.function.Consumer;publicclassSampleConsumer{publicstaticvoidmain(String[] args){Consumer<String> stringConsumer =System.out::println;Consumer<String> stringConsumer1 = vy ->System.out.println(vy);
stringConsumer.accept("Nguyen Bich Van");
stringConsumer1.accept("Tran Phuong Ly");var map =newHashMap<String,Integer>();BiConsumer<String,Integer> stringIntegerBiConsumer = map::put;BiConsumer<String,Integer> stringIntegerBiConsumer1 =(ke, va)-> map.put(ke, va);
stringIntegerBiConsumer.accept("pigeon",2);
stringIntegerBiConsumer1.accept("pig",4);System.out.print("map = ");System.out.println(map);}}
Result
Nguyen Bich Van
Tran Phuong Ly
map = {pigeon=2, pig=4}
Nguồn: viblo.asia