Rule1: Prefer LAMBDAS OVER Aninymous classes
Anonymous classes are meant to provie conciseness over traditional classes and it is expression having been instantialed at same time.Anonymous classes are created to perform some specific task.
Anonymous Inner Class:
Declaration Rule – It’s a class with no name so it must be implementing or extenfing another class.
Example 1:
anonymous class that extends another class:
Thread t=new Thread(){
public void run(){
syso(“ehatever thread does”);
}
}
t.start();
Example 2:
Anonymous class that implements an Interface;
Runnable ra=new Runnable(){
public void run(){
syso(“Thread working”);
}
}
//Other accompanyaing code
Thread t=new Thread(ra);
t.start();
replacing it with lambda :
Runnable ra=()->{
public void run(){
//usual stiuff
}
}
Source:ACTS
Structure of Lambda Expressions
- A lambda expression can have zero, one or more parameters.
- The type of the parameters can be explicitly declared or it can be inferred from the context.
e.g. (int a) is same as just (a) - Parameters are enclosed in parentheses and separated by commas.
e.g. (a, b) or (int a, int b) or (String a, int b, float c) - Empty parentheses are used to represent an empty set of parameters.
e.g. () -> 42 - When there is a single parameter, if its type is inferred, it is not mandatory to use parentheses.
e.g. a -> return a*a - The body of the lambda expressions can contain zero, one or more statements.
If body of lambda expression has single statement curly brackets are not mandatory and the return type of the anonymous function is the same as that of the body expression.
Difference between Lambda Expression and Anonymous class
- One key difference between using Anonymous class and Lambda expression is the use of “this” keyword.
For anonymous class ‘this’ keyword resolves to anonymous class, whereas for lambda expression ‘this’ keyword resolves to enclosing class where lambda is written.
- Another difference between lambda expression and anonymous class is in the way these two are compiled.
Java compiler compiles lambda expressions and convert them into private method of the class.
If you wanna create functional object before java 8 ,you have create method in abstract class or by using anonymous class and accessing the function with object of anonymous class.
the verbosity of this type was unappealing and then came lamdas with with you can create functional object from functional Object (aspect of functtional programming).
Before Java 8:
There is no way of defining just a function / method which stays in Java all by itself. There is no way of passing a method as argument or returning a method body for that instance.
With java8 feature – lambda it is possible.
Possible Applications: Lambdas funtion is an expression which is assigned to functional interface.
1.Application -:Lambdas can be used in place of anonymous classes like:
2.Application-2:Lamndas can be used in case of Enum’s constant specific behaviour,it makes it more concise.
Note- Arguments passed to enum constructor ARE EVALUATED IN STATIC CONTEXT.So, lambdas in a enum constructor canot access instance members of enum.
Usage:
Note:
Resource https://www.concretepage.com/java/java-8/java-functional-interface
Functional interface can be initialized (assigned a value/behaviour) with lambda Expression, method reference and constructor references.
and instance(object ) of Functional Interface can be created by above 3 way (you cannot use new key word..compile time error)
HowToImplementInProduction:
Resource : https://www.concretepage.com/java/java-8/java-functional-interface example-2
Step 1:Create a new method (in case existing method’s lambda expression doesn’t exceed 3 lines)having same name as existing one that takes functional interface of similar type what is returned by existing method.
Step 2:call the newly created method using object of class(with new method,the object will hold getters setter or the values) with lambda expression in it
example:
a.valuedConstructorObject.functionWtihFunctionalInterfaceThatTakesValues()
b.functionWtihFunctionalInterfaceThatTakesValues(valueorList,functionalInterface reference) e.g. Collections.sort(list,expression)
Note- better to use it with libraby Functional Interface(java.util.function) to reduce Conceptual surface are and ease of implementation
THUMB RULE: Lambdas are ideal for 1 line of code and .the three lines is resonably maximum. If you violate this rule. it will casues serious harm to readibility of your programs.