
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pass Lambda Expression as a Method Argument in Java
In this article, we will learn to pass a lambda expression as a method argument in Java. A lambda expression is a short block of code that takes in parameters and returns a value.
Lambda Expression
Lambda expressions allow passing functionality as a method parameter, reducing the need for anonymous classes. They are often used in functional interfaces, especially when working with collections and streams.
Problem Statement
Given an Arraylist the goal is to reverse the strings present in it. Below is a demonstration of the same ?
Input
("Apple", "Orange", "Grapes")
Output
elppA, egnarO, separG
Reversing Strings in an ArrayList Using Lambda
We have an ArrayList of strings, and we want to reverse each string in the list. We will use a lambda expression to achieve this and pass it as an argument to the forEach method.
Following are the steps to reverse a string in an ArrayList using lambda ?
- 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.
Fruits.forEach((e) -> { String result = ""; for (int i = e.length()-1; i >= 0 ; i--) result += e.charAt(i); System.out.print(result + ", "); });
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,
Time Complexity: O(n * m), where n is the number of elements in the ArrayList and m is the average length of the strings.
Space Complexity: O(n * m), as additional space is used to store the reversed strings during the process.
Conclusion
Lambda expressions in Java provide a powerful and concise way to pass behavior as an argument to methods. They are widely used in modern Java programming, especially with functional interfaces and APIs like Stream.