Lambda Expressions - Java 8 Feature

6776 Mar 20, 2016

Lambda expression is the one of major change in Java 8.0 after Generics, Annotations in Java 5.0. Lambda expressions are anonymous method similar to anonymous classes in Java.

(function (x, y) { return x * y; } )

(arg1, arg2, ...) -> { body of expression}

@FunctionalInterface

List<Student>



package com.groupkt.lambda;



public class Student {

private Integer rollNumber;

private String name;

private Integer age;



/**

* @param rollNumber

* @param name

* @param age

*/

public Student(Integer rollNumber, String name, Integer age) {

super();

this.rollNumber = rollNumber;

this.name = name;

this.age = age;

}

/**

* @return the rollNumber

*/

public Integer getRollNumber() {

return rollNumber;

}

/**

* @param rollNumber the rollNumber to set

*/

public void setRollNumber(Integer rollNumber) {

this.rollNumber = rollNumber;

}

/**

* @return the name

*/

public String getName() {

return name;

}

/**

* @param name the name to set

*/

public void setName(String name) {

this.name = name;

}

/**

* @return the age

*/

public Integer getAge() {

return age;

}

/**

* @param age the age to set

*/

public void setAge(Integer age) {

this.age = age;

}



@Override

public String toString() {

return new StringBuilder()

.append("{rollNumber:").append(rollNumber)

.append(",name:").append(name)

.append(",age:").append(age)

.append("}

")

.toString();

}

}



Solution 1: By Creating a class which implements Comparable



/**

* comparator to sort by Students list by roll number

*/

private static class StudentRollNumberComparator implements Comparator<Student> {

@Override

public int compare(Student student1, Student student2) {

return student1.getRollNumber().compareTo(student2.getRollNumber());

}

}



/**

* sort the list of students by using the Implementing class of

* Comparable interface

* @param students : list of student to be sorted

*/

private static void sort(List<Student> students) {

Collections.sort(students, new StudentRollNumberComparator());

}



StudentRollNumberComparator

sort



package com.groupkt.lambda;



import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;



public class Test {



public static void main(String[] args) {

List students = new ArrayList<Student>();



// create a list of students

students.add(new Student(2, "Amit", 16));

students.add(new Student(4, "Kumar", 15));

students.add(new Student(3, "Durgesh", 17));

students.add(new Student(1, "Rajendra", 14));



// call method to sort the list

sort(students);



System.out.println(students);



}



/**

* comparator to sort by Students list by roll number

*/

private static class StudentRollNumberComparator implements Comparator<Student> {

@Override

public int compare(Student student1, Student student2) {

return student1.getRollNumber().compareTo(student2.getRollNumber());

}

}



/**

* sort the list of students by using the Implementing class of

* Comparable interface

* @param students : list of student to be sorted

*/

private static void sort(List<Student> students) {

Collections.sort(students, new StudentRollNumberComparator());

}

}



Solution 2: By using a anonymous class which implements Comparable



/**

* sort the list of students by using anonymous class implementing

* Comparable interface

* @param students : list of student to be sorted

*/

private static void sort(List<Student> students) {

Collections.sort(students,new Comparator<Student>() {

@Override

public int compare(Student student1, Student student2) {

return student1.getRollNumber().compareTo(student2.getRollNumber());

}

});

}



Solution 3: By using a Lambda expression



private static void sort(List<Student> students) {

Comparator<Student> rollNumberComparator = (student1, student2) ->

student1.getRollNumber().compareTo(student2.getRollNumber());



Collections.sort(students,rollNumberComparator);

}



(student1, student2) -> student1.getRollNumber().compareTo(student2.getRollNumber());

Comparator<Student>



[{rollNumber:1,name:Rajendra,age:14}

, {rollNumber:2,name:Amit,age:16}

, {rollNumber:3,name:Durgesh,age:17}

, {rollNumber:4,name:Kumar,age:15}

]



Related Links

Please enable JavaScript to view the comments powered by Disqus.

Disqus

One of great feature Java 8.0 is Lambda expression. JavaScript already has this functionality to create anonymous functions. Lambda expression allows us to create an anonymous methods in Java as well. Lets understand what is Lambda expression in Java1. It is a method without access modifier2. It is a method without return type declaration3. It is a method without nameFor exampleis a Java Lambda expression which takes two parameters and return the multiplication of these two numbers. This expression does not have access modifier (private/protected/public or default), return type and its name. same expression can be written in JavaScript likeThe syntax to write Lambda expression is as. Optionally the type of argument also can be specified. e.g.Before moving further first let us understand. Java already has a Marker Interface which is an interface without any method in it. Similar to marker interface, Functional Interface can have only one abstract method. some of examples arewhich has exactly one methodwhich has only one method. Java 8.0 introduced an annotationwhich can be used to indicate an interface is Functional Interface.We will create a simple test class to understand Lambda expression.Suppose we want to sort a list of Studentsby rollNumberThere are different ways to implement this requirementHere we created a classwhich implements Comparator interface. and creates a methodwhich will sort the list.Complete class to sort the list by student's roll number by using a class which implements Comparator interfaceThis approach will solve our purpose. Here we created a class which implements Comparator.Now let us try another approach to implement the requirement.the method test can be replaced by the code as below, here we are not creating a class but directly creating an anonymous class.This will also sort the students list, but here we have are using anonymous class.Actually this anonymous class is implementing Comparator interface which is FunctionalInterface, and so can be replaced with the Lambda expression.Lambda expression can be used with any functional interface and Comparator is a functional interface, so this test method can be replaced with lambda expression as belowHere we have a lambda expressionwhich takes two student objects to compare the rollNumbers. In this expression we not specifying the type of student1 and student2 because compiler can determine the parameter types by the. In this Lambda expression1. we have not implemented any Interface,2. we have not created any class,3. without method name,4. without method return type,5. without parameters typesAll of thee solutions will give same result