Lambda Expressions

 Lambda Expressions in Java: An Introduction

Lambda expressions, also known as anonymous functions, are a feature added to Java 8 that allow for the creation of small, single-use functions without the need for a name or class. They can be used to implement instances of functional interfaces, which are interfaces with only one abstract method. An example of a functional interface is the java.lang.Runnable interface.

Lambda expressions can implement only one abstract function, and therefore are used to implement functional interfaces with a single abstract method. For example, the Predicate interface is a functional interface with only one abstract method, test(). Here is an example of the Predicate interface in code:

java
interface Predicate { abstract boolean test(T t); }

This interface has one abstract method called test() that takes a single parameter of type T and returns a boolean value. It is a generic method that takes a type parameter.

Instead of creating classes with multiple functions, a lambda expression can be used to implement a functional interface anywhere in a program. For example, the Runnable interface is used only for multithreading and requires the implementation of only a run() method. Similarly, the Comparable interface can be implemented using the compare() method.

Important Points to Remember

  • The body of a lambda expression can contain zero, one, or more statements.
  • When there is a single statement, curly brackets are not mandatory, and the return type of the anonymous function is the same as that of the body expression.
  • When there are more than one statements, then these must be enclosed in curly brackets (a code block), and the return type of the anonymous function is the same as the type of the value returned within the code block, or void if nothing is returned.

Examples

Example 1: Printing Even Numbers in an Array

Here is an example of a naive method to print the even numbers in an array using a functional interface but not a lambda expression:

java
import java.util.*; import java.util.function.Predicate; class GfG { static void printCond(Collection<Integer> c, Predicate<Integer> p) { for (Integer x: c) { if (p.test(x)) { System.out.print(x + " "); } } } public static void main(String args[]) { class MyPredicateEven implements Predicate<Integer> { public boolean test(Integer x) { return (x % 2 == 0); } } List<Integer> al = new ArrayList<>(Arrays.asList(10, 5, 20, 7, 30)); printCond(al, new MyPredicateEven()); } }

In this code, the functional interface Predicate is used to print only the even numbers in an ArrayList. The MyPredicateEven class is used to implement the Predicate interface, and the printCond() function takes the ArrayList and MyPredicateEven object as arguments to print the even numbers.

Example 2: Printing Even Numbers in an Array with Anonymous Classes

Here is another example of a naive method to print the even numbers in an array using a functional interface and anonymous classes but not a lambda expression:

java
import java.util.*; import java.util.function.Predicate; class GfG { static void printCond(Collection<Integer> c, Predicate<Integer> p) { for (Integer x: c) { if (p.test(x)) { System.out.print(x + " "); } } } public static void main(String args[]) { Predicate<Integer> MyPredEven = new Predicate<Integer>() { public boolean test(Integer x) { return (x % 2 == 0); } };
List<Integer> al = new ArrayList<>(Arrays.asList(10, 5, 20, 3, 15)); Collections.sort(al); System.out.println(al);
Previous Post Next Post