Java Lambda Expressions


Java Lambda Expressions

Lambda Expressions has been added to Java 8.

The lambda expression is a short block of code that takes the parameters and returns the value. Lambda expressions are similar to the tracks, but they do not need a name and can be used directly on the road body.


Syntax

A simple lambda quote contains one parameter and a saying:


parameter -> expression

To use more than one parameter, wrap it in brackets:


(parameter1, parameter2) -> expression

Expressions are limited. They should return the amount immediately, and should not contain variable, tasks or statements such as if or for. To perform complex tasks, a code block can be used with curved steel. If the lambda expression needs to be returned to the value, then the code block should have a return statement.


(parameter1, parameter2) -> { code block }


Using Lambda Expressions

Lambda expressions are often transmitted as parameters to a function:


Example

Use a lamba expression in the ArrayList's forEach() method to print every 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 kept flexible if the type of flexibility is a one-way connection. The lambda expression should have the same number of parameters and the same type of return as that. Java has many of these types of built-in collaborative sites, such as the Consumer interface (available in the java.util package) that lists.


Example

Use Java's Consumer interface to store a lambda expression in 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 );
              }
            }

To use a lambda quote correctly, the method must have a parameter with a one-way interface as its type. Calling the interface will use the lambda saying:


Example

Create a method which takes a lambda expression as a parameter:

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);
            }
          }