Java Program to Iterate ArrayList using Lambda Expression

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 : 

  1. Create an ArrayList
  2. Add elements to the ArrayList using the add() method
  3. Get the size of the ArrayList using the size() method
  4. 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
  5. 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

Example 1 : 

Run
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 : 

Run
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
        MyFunction printItem = (item) -> {
            System.out.println(item);
        };
        
        // Iterate over the ArrayList using the custom functional interface
        list.forEach(printItem::apply);
    }
}

Output :

code
java
python

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription