Translate

Search This Blog

Thursday 16 April 2020

Java8 - Functions



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. 
Why do we need a Function interfaces?
  • 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.
How is it used?

  • Functions are functional interface and hence it finds it usages in places where it can be used as Lamda expressions.
What are the various functions present in the functions package?
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.
3. Supplier
  • Represents a supplier of results
  • This interface has the following methods.
    • T get() - gets the result.
4. Predicate
  • 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:

No comments:

Post a Comment

Please use proper blogging etiquette while posting comments.

Note: only a member of this blog may post a comment.