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 helps us to understand Lambda Expression.
For example, If we write the code of the thread in Java 7 it looks like:
- import java.lang.*;
- public class old_Thread extends Thread
- {
- public void run()
- {
- System.out.println("Thread running in Java 7");
- }
- public static void main(String args[])
- {
- old_Thread obj = new old_Thread();
- obj.start();
- }
- }
Output
In Java 8: Now, the same things in Java 8 with the use of Lambda Expression, it looks like:
- import java.lang.*;
- public class lambThread extends Thread
- {
- public static void main(String args[])
- {
- new Thread( () -> System.out.println("Thread running with Lambda expression ")).start();
- }
- }
Output
Example: Let's use another example of runnable code in Java 7.
- import java.lang.*;
- public class old_Run extends Thread implements Runnable
- {
- public void run()
- {
- System.out.println("Thread running in Java 7 with Interface");
- }
- public static void main(String args[])
- {
- old_Run obj = new old_Run();
- obj.start();
- }
- }
Output
In Java 8: Now, the same things in Java 8 using a Lambda Expression, it looks like:
- public class lambRun extends Thread implements Runnable
- {
- public static void main(String args[])
- {
- Runnable r = () -> System.out.println("Thread Run with Lambda Expression using Runnable Interface");
- r.run();
-
- }
- }
Output
Example: In Java 8, we can use a double colon (: :) for printing as in the following:
- import java.util.*;
- public class lamb_colon
- {
- public static void main(String args[])
- {
- String[] atp = {"M S Dhoni", "Sachin Tendulkar", "Virat Kholi", "Suresh Raina", "Zahir Khan", "Irfan Pathan", "Harbajan Singh", "Asheesh Nehra"};
- List<String> players = Arrays.asList(atp);
- System.out.println("");
-
- players.forEach(System.out::println);
-
- }
- }
Output
Example: Let's use an example of looping, the code in Java 7.
- import java.util.*;
- public class loop
- {
- public static void main(String args[])
- {
- String[] atp = {"M S Dhoni", "Sachin Tendulkar", "Virat Kholi", "Suresh Raina", "Zahir Khan", "Irfan Pathan", "Harbajan Singh", "Asheesh Nehra"};
- List<String> players = Arrays.asList(atp);
- System.out.println("");
-
- System.out.println("Print with Java 7 Looping");
- for (String player : players)
- {
- System.out.println(player);
- }
- }
- }
Output
In Java 8: Now, the same thing in Java 8 using a Lambda Expression, it looks like:
- import java.util.*;
- public class lamb_func
- {
- public static void main(String args[])
- {
- String[] atp = {"M S Dhoni", "Sachin Tendulkar", "Virat Kholi", "Suresh Raina", "Zahir Khan", "Irfan Pathan", "Harbajan Singh", "Asheesh Nehra"};
- List<String> players = Arrays.asList(atp);
- System.out.println("");
-
- players.forEach((p) -> System.out.println(p + "; "));
-
- }
- }
Output
Example: Let's use an example of sorting, Code in Java 7.
- import java.util.*;
- import java.util.Arrays;
- public class sorting
- {
- public static void main(String args[])
- {
- String[] players = {"M S Dhoni", "Sachin Tendulkar", "Virat Kholi", "Suresh Raina", "Zahir Khan", "Irfan Pathan", "Harbajan Singh", "Asheesh Nehra"};
- Arrays.sort(players);
- System.out.println("");
- System.out.println("Print with Java 7 sorting");
- for(String player : players)
- {
- System.out.println(player);
- }
- }
- }
Output
In Java 8: Now, the same thing in Java 8 using a Lambda Expression, it looks like:
- import java.util.*;
- import java.util.Arrays;
- public class lamb_sort
- {
- public static void main(String args[])
- {
- String[] players = {"M S Dhoni", "Sachin Tendulkar", "Virat Kholi", "Suresh Raina", "Zahir Khan", "Irfan Pathan", "Harbajan Singh", "Asheesh Nehra"};
- List<String> player = Arrays.asList(players);
- System.out.println("");
-
- Comparator<String> sortByName = (String s1, String s2) -> (s1.compareTo(s2));
- Arrays.sort(players, sortByName);
-
- Arrays.sort(players, (String s1, String s2) -> (s1.compareTo(s2)));
- player.forEach(System.out::println);
- }
- }
Output
Example: This example shows the use of the Filter method in Java 8 using a Lambda Expression.
- import java.io.*;
- import java.util.*;
- import java.util.function.Predicate;
- public class lamb_filter
- {
- public static void main(String [] a)
- {
- List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
- System.out.println("List Contain");
- evaluate(list, (n)->true);
- System.out.println("Empty List");
- System.out.println("");
- evaluate(list, (n)->false);
- System.out.println("Even Numbers are:");
- evaluate(list, (n)-> n%2 == 0 );
- System.out.println("Odd Numbers are:");
- evaluate(list, (n)-> n%2 == 1 );
- System.out.println("Numbers smaller than 3 are:");
- evaluate(list, (n)-> n < 3 );
- System.out.println("Numbers greater than 3 are:");
- evaluate(list, (n)-> n > 3 );
- }
- public static void evaluate(List<Integer> list, Predicate<Integer> predicate)
- {
- for(Integer n: list)
- {
- if(predicate.test(n))
- {
- System.out.println(n + " ");
- }
- }
- }
- }
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.