Iterate Over ArrayList Using Lambda Expression in Java



This article will explain how to iterate over ArrayList using lambda expression. The ArrayList class is a resizable array that can be found in java.util package. The difference between a built-in array and an ArrayList in Java is that the size of an array cannot be modified.

Problem Statement 

Write a Java program to iterate over an ArrayList using lambda expressions. Below is a demonstration of the same ?

Input
Run the program

Output

The list is defined as: Java Python Scala Mysql Redshift

Approaches to find length of the longest substring without repeating characters

Approach 1: Basic Iteration in Main

Below are the steps where we bind all the operations together under the main method ?

  • START
  • Declare an ArrayList to store the elements.
  • Initialize the ArrayList and add elements to it.
  • Use a lambda expression with the forEach method to print each element.
  • STOP

Example

import java.util.ArrayList;
public class Demo {
 public static void main(String[] args) {
System.out.println("The required packages have been imported");
ArrayList<String> input_list = new ArrayList<>();
input_list.add("Java");
input_list.add("Python");
input_list.add("Scala");
input_list.add("Mysql");
input_list.add("Redshift");
System.out.print("\nThe list is defined as: ");
input_list.forEach((e) -> {
 System.out.print(e + " ");
});
 }
}

Output

The required packages have been imported
The list is defined as:
Java Python Scala Mysql Redshift

Approach 2: Encapsulating Operations in Function

Below are the steps where we encapsulate the operations into functions exhibiting object-oriented programming ?

  • START
  • Declare an ArrayList and a function to handle printing.
  • Initialize the ArrayList and add elements.
  • Use a separate function to iterate over the ArrayList and display elements.
  • Call the function to print the list.
  • STOP.

Example

import java.util.ArrayList;
public class Demo {
 static void print(ArrayList<String> input_list){
System.out.print("\nThe list is defined as: ");
input_list.forEach((e) -> {
 System.out.print(e + " ");
});
 }
 public static void main(String[] args) {
System.out.println("The required packages have been imported");
ArrayList<String> input_list = new ArrayList<>();
input_list.add("Java");
input_list.add("Python");
input_list.add("Scala");
input_list.add("Mysql");
input_list.add("Redshift");
print(input_list);
 }
}

Output

The required packages have been imported
The list is defined as: Java Python Scala Mysql Redshift

Conclusion

This article demonstrated how to iterate over an ArrayList using lambda expressions in Java. We explored two approaches: one with direct iteration within the main method and another by encapsulating the iteration logic in a separate method. Both methods efficiently showcase the use of lambda expressions to process and display elements of an ArrayList.

Updated on: 2024-07-24T16:39:07+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements