Java Program to Iterate ArrayList using Lambda Expression
What is ArrayList in Java ?
In Java, an ArrayList is a class that provides a resizable array implementation of the List interface. It allows you to store and manipulate a collection of objects in a dynamic manner, with the ability to add, remove, and access elements by their index.
Unlike a regular array, an ArrayList can grow and shrink in size dynamically as elements are added or removed. This makes it a more flexible data structure that can adapt to changing needs.
What is Lambda Expression ?
A lambda expression in Java is a way to write a concise and expressive version of an anonymous function. It allows you to define a method that can be passed around as an argument to other methods, or returned from methods like a regular value.
Lambda expressions were introduced in Java 8 as a way to provide a more functional programming style in Java. They are essentially a shorthand way of writing anonymous classes that implement a single abstract method, which is often referred to as a functional interface.
Syntax :
(parameter1, parameter2, ...) -> { statements }
Steps to Iterate over an ArrayList :
- Create an ArrayList
- Add elements to the ArrayList using the add() method
- Get the size of the ArrayList using the size() method
- Iterate over the elements in the ArrayList using a for loop:
a. Get the current element using the get() method
b. Do something with the current element - End the loop
Here’s a pseudo-code of the algorithm :
1. ArrayList myList = new ArrayList 2. myList.add("element1") myList.add("element2") myList.add("element3") 3. int size = myList.size() 4. for i = 0 to size - 1 a. String currentElement = myList.get(i) b. // Do something with currentElement 5. end for
Inside the loop, we get the current element using the get() method and then do something with it. This could be printing the element to the console, manipulating it in some way, or anything else that your program needs to do.
When the loop is complete, we have iterated over all elements in the ArrayList.
Example 1 :
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList< String> list = new ArrayList(); list.add("stack"); list.add("array"); list.add("linkList"); // Iterate over the ArrayList using a Lambda expression list.forEach((item) -> { System.out.println(item); }); } }
Output :
stack array linkList
Example 2 :
import java.util.ArrayList; interface MyFunction< T > { void apply(T t); } public class Main { public static void main(String[] args) { ArrayList< String> list = new ArrayList< String>(); list.add("code"); list.add("java"); list.add("python"); // Define a lambda expression using a custom functional interface MyFunctionprintItem = (item) -> { System.out.println(item); }; // Iterate over the ArrayList using the custom functional interface list.forEach(printItem::apply); } }
Output :
code java python
We define a lambda expression that implements this interface, which simply prints out each element of the ArrayList using System.out.println(). Then, we use the forEach method of the ArrayList to iterate over its elements, passing the lambda expression as an argument using the method reference syntax printItem::apply.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment