Lambda in Java8

A lambda expression is a concise representation of anonymous function that can be passed around. It has following characteristics:

  • Anonymous: It does not have a name like we have in method
  • Function: We call lambda function as its not associated with any class
  • Passed Around:A lambda expression can be passed to a method as an argument or it can be stored in a variable
  • Concise:We dont need to write boilerplate code like we used to do for anonymous classes.

For Example lets see how lambda has changed are way of writing code


Comparator byId = new Comparator() {
 public int compare(Employee e1, Employee e2){
 return e1.getId().compareTo(e2.getId());


Comparator byId = (Employee e1,Employee e2)-> e1.getId().compareTo(e2.getId());

Lambda expression are used to bind implementation with FunctionalInterfaceC

There is important concept in lambda about Type Checking, Type Inference and MethodReference.Lets discuss that.

  1. Type Checking: Lets understand this by example ,we have 2 functionalInterface Callable and PriviledgeAction both accepts nothing and returns generic type T.
Callable c = () -> 12; 
PrivilegedAction p = () -> 12;

Here both lambda expressions are same but one is giving return type as Callable and another as PrivilegedAction, now how this works.

Lambda expression does not contain any information which functional Interface its implementing .The type of a lambda is deduced from the context in which the lambda is used. In above example target type is Callable for first lambda and for second is PrivilegeAction, so based on the target type Type checking is done in lambda.

In Java 7 the Idea of Types being concluded from context with generic inference was introduced using the Diamond Operator(<>). Like below

List listOfStrings = new ArrayList<>();
List listOfIntegers = new ArrayList<>();

Now lets look at Type Inference

2. Type Inference: By Type Inference we mean, we can omit the types for variable when using lambda expression as Java compiler concludes the types of the parameters on its own based on the functional Interface it associate with the lambda expression . see below

//Without Type Inference
Comparator cmp2 = (Employee e1, Employee e2)->e1.getId().compareTo(e2.getId());
//With Type Inference
Comparator cmp = (e1,e2)->e1.getId().compareTo(e2.getId());

3. MethodReference(Double Colon Operator :: ):Method Reference is basically shorthand for lambda calling only a specific method by name.

With Lambda

Function<String,Integer> func1 = (s) -> (Integer.parseInt(s));

Without Lambda Using Method Reference(::)

Function<String,Integer> func1 = (s) -> Integer::parseInt;

Only thing to remember when using Method Reference is the Method you provide in Method Reference should have a same signature to the method of the functional interface which you use as object reference. In above example Object Reference Type is Function<T,R> which takes input String and return Integer .

There are 3 types of method Reference :

1.Method Reference to a static Method

2 Method Reference to instance Method of an arbitrary type

3.Method Reference to instance of an existing object

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *