Sunday, August 24, 2014

Lambda Expression in Java

In simple terms, Lambda expression is just a block of code which can be passed as an argument. Java takes its pride in being a full object oriented language but then there are merits to functional programming. Before Java 8, when lambda expressions were not there, one has achieve the same by passing either Anonymous classes. Lambda expression needs Functional Interfaces to work. You can see details of Functional Interface here.

Let's see with an example. Let's define a class which uses comparator interface to compare the objects on different attributes.

public class Student {


int rollNumber;
int marks;

public Student(int rollNumberValue, int marksValue){
rollNumber = rollNumberValue;
marks = marksValue;
}

public int getRollNumber() {
return rollNumber;
}

public int getMarks() {
return marks;
}

//Pass a comparator interface and this will be defined as per context.
public boolean isGreaterThan(Student otherStudent, Comparator comparator) {
int returnValue = comparator.compare(this, otherStudent);
if(returnValue >= 0){
return true;
}else{
return false;
}
}
}

Now if you see, we have defined a generic function isGreaterThan which based on the implementation of comparator interface passed. Let's see in the main method how we can compare two student objects based on roll number and marks

//Let's make two Student object
//One the first roll number and higher marks 
//Other have high roll number but less marks
Student studentOne = new Student(1,80);
Student studentTwo = new Student(20,40);

//Let's compare if studentOne roll number is greater
boolean status = studentOne.isGreaterThan(studentTwo, new Comparator(){

@Override
public int compare(Object o1, Object o2) {
Student studentA = (Student)o1;
Student studentB = (Student)o2;
if(studentA.getRollNumber() >= studentB.getRollNumber()){
return 1;
}
return -1;
}

});

System.out.println("StudentOne Roll number is greater than student Two: " + status);

//Now let's compare the marks
 status = studentOne.isGreaterThan(studentTwo, new Comparator(){

@Override
public int compare(Object o1, Object o2) {
Student studentA = (Student)o1;
Student studentB = (Student)o2;
if(studentA.getMarks() >= studentB.getMarks()){
return 1;
}
return -1;
}

});

System.out.println("StudentOne marks is greater than student Two: " + status);


Note that we have to define the implementation of Comparator interface as an anonymous class. Now let's see how we can implement the same using lambda expression. As comparator interface is a functional interface, we can use that for defining lambda expression.

//Now let's use lambda expression for roll number case
status = studentOne.isGreaterThan(studentTwo, (Object o1,Object o2)->{
Student studentA = (Student)o1;
Student studentB = (Student)o2;
if(studentA.getRollNumber() >= studentB.getRollNumber()){
return 1;
}
return -1;
 });
System.out.println("StudentOne Roll number is greater than student Two: " + status);

//Let's use lambda expression for marks case
status = studentOne.isGreaterThan(studentTwo, (Object o1,Object o2)->{
Student studentA = (Student)o1;
Student studentB = (Student)o2;
if(studentA.getMarks() >= studentB.getMarks()){
return 1;
}
return -1;
 });

System.out.println("StudentOne marks is greater than student Two: " + status);

If you notice the syntax of lambda expression is:

(argument List) -> { body of code to be executed}

The argument list can be 0 or more and if the type can be inferred, then we do not have to specify type also.

No comments:

Post a Comment