Java Lambda
Lambda Expressions
In Java8, lambda expressions came about to allow Java to evolve and provide:
more expressive programming languange
better libraries
reduce code verbosity with concise code
increased data parallelism
Lambda in Java has proved to be so popular that it has mostly taken over anonymous classes. A special JVM byte-code called invokedynamic was introduced so lambdas could be implemented using method handles. This is one of the reasons you need to have a Java8 javac and JVM.
Lambda expressions create target types know as functional interfaces which must:
be interfaces
have only one non-default method (a mandatory method). They can have other methods that are default.
Functional Interface
A functional interface is an interface with a single abstract method that is used as the type in a lambda expression. Because it only has one method, when using it with lambda expressions, you do not have to specify the function name. It is automatically inferred.
The
@FunctionalInterface
annotation can be used when the interface violates contracts of the Functional Interface. It is only for informing the compiler to enforce single abstract method (SAM) inside the interface.If the interface defines a method that overrides its java.lang.Object parent method, it does not count towards the method count since all implementations of the interface will have an implementation from java.lang.Object. See the example given below.
@FunctionalInterface
Public interface MyFirstFunctionalInterface {
public void firstWork();
@Override
public String toString();
@Override
Public Boolean equals(Object obj);
}