Java 8 – Lambda Expression example

Lambda Expression

  • Lambda expression is a new feature in Java 8 .
  • Alternative to anonymous class implementation of interfaces ( with single abstract method ).
  • syntax : (parameters) -> { body of statements }

Example – Sorting the list of Employees using comparator

Lets use the following employee class objects for adding to the list.

Old way ( before Java 8 )

Using Lambda expression ( available from Java 8 )

Output

Sorted by Name :
Name : Hari , Salary : 2000
Name : Naveen , Salary : 1000
Name : Raju , Salary : 9000

 

References

Java 8 – new features and enhancements with examples

http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html

Author of CodeForEach.com . Loves coding and related technologies.
 

www.000webhost.com