How many parameters can a lambda expression have in Java?


The lambda expressions are easy and contain three parts like parameters (method arguments), arrow operator (->) and expressions (method body). The lambda expressions can be categorized into three types: no parameter lambda expressions, single parameter lambda expressions and multiple parameters lambda expressions.

Lambda Expression with no parameter

We need to create no parameter lambda expression then start the expression with empty parenthesis.

Syntax

() -> {
   //Body of no parameter lambda
}

Example of no parameter Lambda

import java.util.function.*;
import java.util.Random;

public class LambdaExpression1 {
   public static void main(String args[]) {
      NumberUtil num = new NumberUtil();
      int randVal = num.getRandomValue(
               () -> {     // no parameter lambda
                   Random random = new Random();
                   return random.nextInt(500);
               });
      System.out.println("The value returned from Lambda is: "+ randVal);
   }
   private static class NumberUtil {
      public int getRandomValue(Supplier<Integer> supplier) {
         return supplier.get();
      }
   }
}

Output

The value returned from Lambda is: 144


Lambda Expression with single Parameter

We need to create lambda expression with a single parameter then start the expression with a parenthesis of a single argument.

(p1) -> {
   //Body of single parameter lambda
}

Example of a single parameter Lambda

import java.util.function.*;

public class LambdaExpression2 {
   public static void main(String args[]) {
      Validator validator = new Validator();
      String city = "Hyderabad";
      boolean isValid = validator.isDataValid(city,
                    (String info) -> {           // single parameter lambda
                       String regx = "^[a-zA-Z0-9]*$";
                       return info.matches(regx);
                    });
      System.out.println(" The value returned from lambda is: "+ isValid);
   }
   private static class Validator {
      public boolean isDataValid(String data, Predicate<String> predicate) {
         return predicate.test(data);
      }
   }
}

Output

The value returned from lambda is: true


Lambda Expression with multiple parameters

We need to create lambda expression with multiple parameters then start the expression with parenthesis of multiple arguments.

(p1,p2) -> {
   //Body of multiple parameter lambda
}

Example of multiple parameters Lambda

import java.util.function.*;

public class LambdaExpression3 {
   public static void main(String args[]) {
      Message m = new Message();
      m.printStringInteger("Java", 75,
                    (String str, Integer number) -> {     // multiple parameters lambda
                       System.out.println("The values are: "+ str + " "+ number);
                    });
      }
   private static class Message {
      public void printStringInteger(String str, Integer number, BiConsumer<String, Integer> biConsumber) {
         biConsumber.accept(str, number);
      }
   }
}

Output

The values are: Java 75
raja
Published on 05-Dec-2019 10:38:58
Advertisements