# Java Program to pass lambda expression as a method argument

JavaObject Oriented ProgrammingProgramming

In this article, we will understand how to pass lambda expression as a method argument. A lambda expression is a short block of code which takes in parameters and returns a value.

Below is a demonstration of the same −

Input

Suppose our input is −

("Apple", "Orange", "Grapes")

Output

The desired output would be −

elppA, egnarO, separG


## Algorithm

Step 1 - START
Step 2 - We import the required packages.
Step 3 - In the main function, we define an ‘ArrayList’ of data.
Step 4 - This is displayed on the console.
Step 5 - Now, a ‘forEach’ loop is used to iterate over the elements of the ArrayList from the end, instead of the beginning.
Step 6 - The element at every index is accessed and incremented by a specific value.
Step 7 - This will result in the ArrayList elements being displayed in reverse order.

## Example 1

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

import java.util.ArrayList;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
ArrayList<String> Fruits = new ArrayList<>(Arrays.asList("Apple", "Orange", "Grapes"));
System.out.println("The ArrayList is defined as : " + Fruits);
System.out.print("The Reversed ArrayList is: ");
Fruits.forEach((e) -> {
String result = "";
for (int i = e.length()-1; i >= 0 ; i--)
result += e.charAt(i);
System.out.print(result + ", ");
});
}
}

## Output

The ArrayList is defined as : [Apple, Orange, Grapes]
The Reversed ArrayList is: elppA, egnarO, separG,

## Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

import java.util.ArrayList;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
ArrayList<String> Games = new ArrayList<>(Arrays.asList("Football", "Cricket", "Baseball"));
System.out.println("The ArrayList is defined as : " + Games );
System.out.print("The Reversed ArrayList is: ");
Games .forEach((e) -> {
String result = "";
for (int i = e.length()-1; i >= 0 ; i--)
result += e.charAt(i);
System.out.print(result + ", ");
});
}
}

## Output

The ArrayList is defined as : [Football, Cricket, Baseball]
The Reversed ArrayList is: llabtooF, tekcirC, llabesaB,
Updated on 22-Feb-2022 10:10:53