Wednesday, July 5, 2017

Java 8 & Lambda Expressions


In JDK 8 a LAMBDA Expression is a way  of defining the anonymous functions. Simple example is as follows.
Consumer<String> consumer = (x) -> System.out.print(x);
Now calling consumer.accept(" Hey There !"); and it will apply the string parameter to the body of the anonymous function.
Result:  Hey There !
We can think of it as a function defined in two parts:
  1. The right part of the expression is the body of the function
  2. The signature of the function is defined in a Functional Interface, in this case the Consumer<T>interface (more about Functional Interfaces below.
One important detail is that the lambda expression is of the same type of the functional interface associated with it.
Lambda Expression vs Anonymous classes.
Need to update ....

0 comments:

Post a Comment