blog posts

Learn Lambda Expressions In Java (Lambda Expressions)

When Java 8 was released, lambda phrases were a hot topic. Lambda expressions have been added to JDK version 8 to enhance Java performance by increasing the language’s expressive power.

But, before entering Lambda, we must first know what the user interface is.

What is a user interface?

If a Java interface contains only one abstract method, it is called a functional interface. For example, the Runnable interface of the package; java.lang is a functional interface because it forms only one method, run ().

Example 1: Define a user interface or interface in Java

  1. import java.lang.FunctionalInterface;
  2. @FunctionalInterface
  3. public interface MyInterface {
  4. double getValue ();
  5. }

Note: Annotating @ FunctionalInterface is not necessary, but it is wise to use it because the Java compiler forces the defined interface to be a functional interface and should only have an abstract method. In Java 7, the interface was called the Abstract Method Single or SAM type.

SAM was usually run with anonymous classes in Java 7.

Example 2: Run SAM with anonymous classes in Java

  1. public class FunctionInterfaceTest {
  2. public static void main (String [] args) {
  3. new Thread (new Runnable () {
  4. @Override
  5. public void run () {
  6. System.out.println (“I just implemented the Runnable Functional Interface.”);
  7. }
  8. }). start ();
  9. }
  10. }

The ability to send an anonymous class to the constructor or method made it easy to write Java 7 code with fewer files. However, the structure was still difficult and a lot of code was needed.

Java 8 went one step further, increasing the power of a SAM. Since we know that an interface has only one method, there is no need to define the name of that method when sending it as an argument. The term lambda allows us to do just that.

Familiarity with the term lambda

The term lambda is essentially an unknown or unspecified method and is not executed alone. Instead, it is used to implement the method defined by the interface.

How to define the term lambda in Java?

The term lambda introduces a new syntactic element and operator in Java. The new operator is called the lambda operator or the arrow operator. (->)

Let’s write a simple method that returns only a constant double value.

double getPiValue () {return 3.1415; }

The lambda expression equivalent to the above method is:

() -> 3.1415

In the lambda expression, the left side of the expression specifies the required parameters of the expression, while the right is the body of the lambda, which determines the function of the lambda expression.

Lambda body is of two types.

1- Body with a single phrase

() -> System.out.println (“Lambdas are great”);

2- The body consists of a block of code

() -> {

double pi = 3.1415;

return pi;

}

A lambda expression can have a parameter. For example:

(n) -> (n% 2) == 0

This lambda expression specifies that the value of n is even or odd.

If the body of the lambda is a block of code, you should always return a value. However, if the lambda body is just a phrase, a return value is not required.

Let’s write the Java code with the lambda expression that returns the value of Pi.

As mentioned earlier, the term lambda is not used alone. Instead, the execution of the abstract method defined by the interface constitutes a function.

Example 3: Define the term lambda with the user interface (interface) in Java

We must first define a MyInterface.java user interface:

  1. import java.lang.FunctionalInterface;
  2. // This is functional interface
  3. @FunctionalInterface
  4. public interface MyInterface {
  5. double getPiValue ();
  6. }

Now, let’s take the lambda expression as an example of a functional interface.

  1. public class LambdaMain {
  2. public static void main (String [] args) {
  3. MyInterface myInterface;
  4. myInterface = () -> 3.1415;
  5. System.out.println (“Value of Pi =” + myInterface.getPiValue ());
  6. }
  7. }

The output is equal to:

Value of Pi = 3.1415

The lambda expression must be consistent with the abstract method. That is, if you assign () -> “3.1415” to the myInterface instance, the code is defective and will not run because by definition in the interface, the string type is not compatible with double.

You probably do not use a lambda phrase like the one above in a real application. Let’s look at another example of a lambda expression that takes parameters.

Example 4: Using the term lambda with parameters in Java

  1. @FunctionalInterface
  2. MyInterface {interface
  3. Reverse string (String n);
  4. }
  5. public class ParamLambdaMain {
  6. public static void main (String [] args) {
  7. MyInterface myInterface = (str) ->
  8. String result = “”;
  9. for (int i = str.length () – 1; i> = 0; i–)
  10. result + = str.charAt (i);
  11. return result;
  12. };
  13. System.out.println (“Lambda reversed =” + myInterface.reverse (“Lambda”));
  14. }
  15. }

Output

Lambda reversed = adbmaL

General functional interface

The above interface only accepts String and returns the String object. However, we can create a public user interface so that any data type can be accepted.

Example 5: How can a user interface accept any type of data in Java?

Let’s see how this is done:

  1. // GenericInterface.java
  2. @FunctionalInterface
  3. interface GenericInterface <T> {
  4. T func (T t);
  5. }

Now, GenericInterface is compatible with any lambda expression that takes a parameter and returns a value of the same type.

  1. // GenericLambda.java
  2. public class GenericLambda {
  3. public static void main (String [] args) {
  4. GenericInterface <String> reverse = (str) -> {
  5. String result = “”;
  6. for (int i = str.length () – 1; i> = 0; i–)
  7. result + = str.charAt (i);
  8. return result;
  9. };
  10. System.out.println (“Lambda reversed =” + reverse.func (“Lambda”));
  11. GenericInterface <Integer> factorial = (n) -> {
  12. int result = 1;
  13. for (int i = 1; i <= n; i ++)
  14. result = i * result;
  15. return result;
  16. };
  17. System.out.println (“factorial of 5 =” + factorial.func (5));
  18. }
  19. }

Output

Lambda reversed = adbmaL

factorial of 5 = 120

Lambda expression and API flow

A new java.util.stream package has been added to JDK8 that allows java programmers to perform operations such as search, filter, map, reduce, or otherwise manipulate collections such as lists.

For example, we have a data stream (a string list) in which each string is a combination of the country name and the country location. Now, we can process this data stream and retrieve only Nepal locations.

Example 6: Demonstrate the use of lambda with the API stream

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class StreamMain {
  4. static List <String> places = new ArrayList <> ();
  5. // preparing our data
  6. public static List getPlaces () {
  7. places.add (“Nepal, Kathmandu”);
  8. places.add (“Nepal, Pokhara”);
  9. places.add (“India, Delhi”);
  10. places.add (“USA, New York”);
  11. places.add (“Africa, Nigeria”);
  12. return places;
  13. }
  14. public static void main (String [] args) {
  15. List <String> myPlaces = getPlaces ();
  16. System.out.println (“Places from Nepal:”);
  17. // Filter places from Nepal
  18. myPlaces.stream ()
  19. .filter ((p) -> p.startsWith (“Nepal”))
  20. .map ((p) -> p.toUpperCase ())
  21. .sorted ()
  22. .forEach ((p) -> System.out.println (p));
  23. }
  24. }

Output

Places from Nepal:

NEPAL, KATHMANDU

NEPAL, POKHARA

The API stream allows us to access methods such as filter (), map, and forEach () that can take a lambda expression as input. We can use both built-in Java methods and also define our own expressions based on the method we learned above. This reduces the lines of code dramatically as we saw in the example above.