Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

Java Lambda

Java Lambda Expressions

Java 8 introduced lambda expressions, which are compact blocks of code that take parameters and return values. Similar to methods, lambda expressions do not require a name and can be directly implemented within the body of a method.

Syntax

The most basic lambda expression consists of a single parameter and an expression.

parameter -> expression

When utilizing more than one parameter, enclose them within parentheses.

(parameter1, parameter2) -> expression

Expressions are constrained; they must promptly return a value and cannot encompass variables, assignments, or statements like if or for. For more intricate operations, a code block enclosed in curly braces can be employed. If the lambda expression necessitates returning a value, the code block should feature a return statement.

(parameter1, parameter2) -> { code block }

Using Lambda Expressions

Lambda expressions are commonly supplied as arguments to functions.

Example

Utilize a lambda expression within the forEach() method of the ArrayList to print each item in the list.

 import java.util.ArrayList;
 public class Main {
  public static void main(String[] args) {
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    numbers.add(5);
    numbers.add(9);
    numbers.add(8);
    numbers.add(1);
    numbers.forEach( (n) -> { System.out.println(n); } );
  }        
}

Lambda expressions can be assigned to variables if the variable’s type is an interface with only one method. The lambda expression must possess the same number of parameters and the same return type as that method. Java includes several built-in interfaces of this type, such as the Consumer interface (found in the java.util package), which is commonly used with lists.

Example

Utilize Java’s Consumer interface to store a lambda expression within a variable.

import java.util.ArrayList;
import java.util.function.Consumer;
 public class Main {
  public static void main(String[] args) {
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    numbers.add(5);
    numbers.add(9);
    numbers.add(8);
    numbers.add(1);
    Consumer<Integer> method = (n) -> { System.out.println(n); };
    numbers.forEach( method );
  }
}

For incorporating a lambda expression within a method, the method should accept a parameter with a single-method interface as its type. Invoking the method of this interface will execute the lambda expression.

Example

Generate a method that accepts a lambda expression as an argument.

interface StringFunction {
  String run(String str);
}
 public class Main {
  public static void main(String[] args) {
    StringFunction exclaim = (s) -> s + "!";
    StringFunction ask = (s) -> s + "?";
    printFormatted("Hello", exclaim);
    printFormatted("Hello", ask);
  }
  public static void printFormatted(String str, StringFunction format) {
    String result = format.run(str);
    System.out.println(result);
  }
}