Introducing functional programming in Java
Functional programming is based on Streams and Lambda expressions, both introduced in Java 8. Libraries such as Retrolambda allow Java 8 code to run on older JVM runtimes, such as Java 5,6, or 7 (typically used for Android development).
Lambda expressions
Lambda expressions are syntax sugar for the use of the java.util.functions
package interfaces. The most important ones are the following:
BiConsumer<T,U>
: An operation that consumes two input arguments and returns no result, usually used in the mapsforEach
method. It has support for chainingBiConsumers
by using theandThen
method.BiFunction<T,U,R>
: A function that accepts two arguments and produces a result, used by calling itsapply
method.BinaryOperator<T>
: An operation upon two operands of the same type, producing a result of the same type as the operands, used by calling its inheritedapply
method. It statically offers theminBy
andmaxBy
methods, which return the lesser/greater...