Lambda Expressions in Java SE 8

Introduction

 
In this article, we will learn how to work with Lambda Expressions.  As we know, a Lambda Expression is one of the most-awaited and hyped functions of Java SE 8.  People have differing points of views of Lambda Expressions.  Before we start, let's understand what Lambda Expressions are.

 

Lambda Expression

 
Simply a Lambda Expression (a new feature in Java SE 8), is a method without declaration like access modifiers, name, return value and declaration.  It allows us to treat code as data and functionality, as method arguments.  In short, it is a block of code with parameters (arguments), that is useful when a method is being used only once, during its life cycle.  It allows us to write a block of code, at the same place where we want to use it.  It replaces anonymous classes and allows us to write code in a functional style.  It also improves Collection libraries.  Some people say that Lambda Expressions are just like Anonymous Classes, but the following are the differences.
  

Differences between Lambda Expression and Anonymous Class

 
Lambda Expression Anonymous Class
In Lambda Expression "this" keyword resolves to the enclosing class In Anonymous Class "this" keyword resolves to the anonymous class
When compiling a Lambda Expression is converted into a private method of the class When compiling, it generates "class_name$some_number.class file"
 
Some of the examples are given below that help us to understand Lambda Expression.
 
For example, If we write the code of the thread in Java 7 it looks like:
  1. import java.lang.*;  
  2. public class old_Thread extends Thread {  
  3.  public void run() {  
  4.   System.out.println("Thread running in Java 7");  
  5.  }  
  6.  public static void main(String args[]) {  
  7.   old_Thread obj = new old_Thread();  
  8.   obj.start();  
  9.  }  
  10. }   
Output
 
 
 
In Java 8: Now, the same things in Java 8 with the use of Lambda Expression, it looks like:
  1. import java.lang.*;  
  2. public class lambThread extends Thread {  
  3.  public static void main(String args[]) {  
  4.   new Thread(() -> System.out.println("Thread running with Lambda expression ")).start();  
  5.  }  
  6. }   
Output
 
 
 
Example:
Let's use another example of runnable code in Java 7.
  1. import java.lang.*;  
  2. public class lambThread extends Thread {  
  3.  public static void main(String args[]) {  
  4.   new Thread(() -> System.out.println("Thread running with Lambda expression ")).start();  
  5.  }  
  6. }   
 Output
 
  
 
In Java 8: Now, the same things in Java 8 using a Lambda Expression, it looks like:
  1. public class lambRun extends Thread implements Runnable {  
  2.  public static void main(String args[]) {  
  3.   Runnable r = () -> System.out.println("Thread Run with Lambda Expression using Runnable Interface");  
  4.   r.run();  
  5.  }  
  6. }   
Output
 
 
Example: In Java 8, we can use a double colon (: :) for printing as in the following:
  1. import java.util.*;  
  2. public class lamb_colon {  
  3.  public static void main(String args[]) {  
  4.   String[] atp = {  
  5.    "M S Dhoni",  
  6.    "Sachin Tendulkar",  
  7.    "Virat Kholi",  
  8.    "Suresh Raina",  
  9.    "Zahir Khan",  
  10.    "Irfan Pathan",  
  11.    "Harbajan Singh",  
  12.    "Asheesh Nehra"  
  13.   };  
  14.   List < String > players = Arrays.asList(atp);  
  15.   System.out.println("");  
  16.   players.forEach(System.out::println);  
  17.  }  
  18. }   
 Output
 
 
 
Example:
Let's use an example of looping, the code in Java 7.
  1. import java.util.*;  
  2. public class loop {  
  3.  public static void main(String args[]) {  
  4.   String[] atp = {  
  5.    "M S Dhoni",  
  6.    "Sachin Tendulkar",  
  7.    "Virat Kholi",  
  8.    "Suresh Raina",  
  9.    "Zahir Khan",  
  10.    "Irfan Pathan",  
  11.    "Harbajan Singh",  
  12.    "Asheesh Nehra"  
  13.   };  
  14.   List < String > players = Arrays.asList(atp);  
  15.   System.out.println("");  
  16.   System.out.println("Print with Java 7 Looping");  
  17.   for (String player: players) {  
  18.    System.out.println(player);  
  19.   }  
  20.  }  
  21. }   
Output
 
 
 
In Java 8: Now, the same thing in Java 8 using a Lambda Expression, it looks like:
  1. import java.util.*;  
  2. public class lamb_func {  
  3.  public static void main(String args[]) {  
  4.   String[] atp = {  
  5.    "M S Dhoni",  
  6.    "Sachin Tendulkar",  
  7.    "Virat Kholi",  
  8.    "Suresh Raina",  
  9.    "Zahir Khan",  
  10.    "Irfan Pathan",  
  11.    "Harbajan Singh",  
  12.    "Asheesh Nehra"  
  13.   };  
  14.   List < String > players = Arrays.asList(atp);  
  15.   System.out.println("");  
  16.   players.forEach((p) -> System.out.println(p + "; "));  
  17.  }  
  18. }   
Output
 
 
 
Example: Let's use an example of a sorting Code in Java 7. 
  1. import java.util.*;  
  2. import java.util.Arrays;  
  3. public class sorting {  
  4.  public static void main(String args[]) {  
  5.   String[] players = {  
  6.    "M S Dhoni",  
  7.    "Sachin Tendulkar",  
  8.    "Virat Kholi",  
  9.    "Suresh Raina",  
  10.    "Zahir Khan",  
  11.    "Irfan Pathan",  
  12.    "Harbajan Singh",  
  13.    "Asheesh Nehra"  
  14.   };  
  15.   Arrays.sort(players);  
  16.   System.out.println("");  
  17.   System.out.println("Print with Java 7 sorting");  
  18.   for (String player: players) {  
  19.    System.out.println(player);  
  20.   }  
  21.  }  
  22. }  
Output
 
 
In Java 8: Now, the same thing in Java 8, using a Lambda Expression, it looks like:
  1. import java.util.*;  
  2. import java.util.Arrays;  
  3. public class lamb_sort {  
  4.  public static void main(String args[]) {  
  5.   String[] players = {  
  6.    "M S Dhoni",  
  7.    "Sachin Tendulkar",  
  8.    "Virat Kholi",  
  9.    "Suresh Raina",  
  10.    "Zahir Khan",  
  11.    "Irfan Pathan",  
  12.    "Harbajan Singh",  
  13.    "Asheesh Nehra"  
  14.   };  
  15.   List < String > player = Arrays.asList(players);  
  16.   System.out.println("");  
  17.   Comparator < String > sortByName = (String s1, String s2) -> (s1.compareTo(s2));  
  18.   Arrays.sort(players, sortByName);  
  19.   Arrays.sort(players, (String s1, String s2) -> (s1.compareTo(s2)));  
  20.   player.forEach(System.out::println);  
  21.  }  
  22. }   
Output
 
 
 
Example: This example shows the use of the Filter method in Java 8, using a Lambda Expression.
  1. import java.io.*;  
  2. import java.util.*;  
  3. import java.util.function.Predicate;  
  4. public class lamb_filter {  
  5.  public static void main(String[] a) {  
  6.   List < Integer > list = Arrays.asList(123456);  
  7.   System.out.println("List Contain");  
  8.   evaluate(list, (n) -> true);  
  9.   System.out.println("Empty List");  
  10.   System.out.println("");  
  11.   evaluate(list, (n) -> false);  
  12.   System.out.println("Even Numbers are:");  
  13.   evaluate(list, (n) -> n % 2 == 0);  
  14.   System.out.println("Odd Numbers are:");  
  15.   evaluate(list, (n) -> n % 2 == 1);  
  16.   System.out.println("Numbers smaller than 3 are:");  
  17.   evaluate(list, (n) -> n < 3);  
  18.   System.out.println("Numbers greater than 3 are:");  
  19.   evaluate(list, (n) -> n > 3);  
  20.  }  
  21.  public static void evaluate(List < Integer > list, Predicate < Integer > predicate) {  
  22.   for (Integer n: list) {  
  23.    if (predicate.test(n)) {  
  24.     System.out.println(n + " ");  
  25.    }  
  26.   }  
  27.  }  
  28. }   
Output
 
 
 

Summary

 
The summary of this article is that we have implemented Lambda Expressions in various ways and learned how to work with them.  It is also helpful to write clean code and it will take Java to the next level.