What are java 8 Functions all about?
- Functions are a new addition in Java 8.
- Are used to implement Functional programming in Java
- Java 8 Function's are Functional interfaces and they have a single abstract method
- For example, the Function interface has a single abstract method apply() .
- There are various flavours of it present in the java.util.function package.
- Its purpose lies in that, it abstracts out the behaviour of a function. i.e a function takes an input and gives an output.
- i.e. broadly map object of one type and converts it to another [R apply(T)],
- For example, the map function in streams converts/maps object of one type to another.
- Note:
- Not all interfaces in the function package, adhere to this rule.
- For example Consumer interface within function package just excepts a value and does not return anything.
- Functions are functional interface and hence it finds it usages in places where it can be used as Lamda expressions.
Following are the main functional interfaces and all others are variations to these interfaces. If you understand these interfaces , you would quickly grasp the rest of the interface functions.
1. Function
- Represents a function that accepts one argument and produces a result.
- This interface has the following functions defined
- R apply(T) - abstract method which takes an argument of type T and returns R
- andThen(Function
after) - its a default method, it combines the Function on which it is applied(T,R) with another function named after (going from R to V), so that the net affect is a composed function which results in transformation from T to V. - compose(Function
before) - its again another default method, it first applies the function(V,T) given as parameter and then applies this function(T,R) to give a combined function whose net effect is a composed function which results in transformation from V to R. - identity() - is a static function which always returns a function that always returns its input argument.
2. Consumer
- Represents an operation that accepts a single input argument and returns no result.
- This interface has the following methods
- void accept(T t) - Performs this operation on the given argument.
- andThen(Consumer
after) - Returns a composed Consumer that performs, in sequence, this operation followed by the after operation.
- Represents a supplier of results
- This interface has the following methods.
- T get() - gets the result.
- Represents a predicate (boolean-valued function) of one argument.
- This interface has the following methods
- boolean test(T t) - and abstract method that evaluates this predicate on the given argument.
- and(Predicate other) - default method that returns a composed predicate that represents a short-circuiting logical AND of this predicate and another.
- or(Predicate other) - default method that returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.
- isEqual(Object other) - static method that returns a predicate that tests if two arguments are equal according to Objects.equals(Object, Object).
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html
- https://www.journaldev.com/2389/java-8-features-with-examples
- https://www.javabrahman.com/java-8/java-8-java-util-function-function-tutorial-with-examples/
- https://www.geeksforgeeks.org/function-interface-in-java-with-examples/
No comments:
Post a Comment
Please use proper blogging etiquette while posting comments.
Note: only a member of this blog may post a comment.