Function programming in Java 17

Tram Ho

Functional interfaces:

InterfaceMethodReturn 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

result

More complete object

or other way

result

How Java virtual machine manage object what is instance of Supplier interface? Try an experiment:

result

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

Result

Share the news now