Java arraylist foreach: In the previous article we have discussed about Java ArrayList iterator() Method with Example
In this article we are going to see the use ArrayList forEach() method along with suitable examples by using Java programming language.
Java ArrayList forEach() Method with Example
forEach():
Foreach arraylist java: This java.util.ArrayList.forEach()
method is used to traverse all the arraylist and performs given action on each element of the arraylist one by one till it processes all the elements..
Syntax:
arrayListName.forEach()
Where,
arrayListName
refers to the name of your ArrayList.
Let’s see different examples to understand it more clearly.
Method-1: Java ArrayList forEach() Method – Example with String Type ArrayList
Approach:
- Create a new ArrayList of type String.
- Add string elements into the ArrayList using the add() method.
- Display the ArrayList elements.
- Now, using the
forEach()
method traverse the arraylist and perform given action for each element one by one. - Print the new arrayList.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create an ArrayList of string datatype ArrayList<String> arr1 = new ArrayList<String>(); // Adding some elements to the ArrayList arr1.add("vivo"); arr1.add("htc"); arr1.add("samsung"); arr1.add("realme"); arr1.add("nokia"); // Prints the ArrayList elements System.out.println("The elements in the ArrayList are: "+arr1); // add 1 on each element with a space using forEach() method System.out.print("The new elements in the ArrayList are: "); arr1.forEach((e) -> {System.out.print(e + " 1 ");}); } }
Output: The elements in the ArrayList are: [vivo, htc, samsung, realme, nokia] The new elements in the ArrayList are: vivo 1 htc 1 samsung 1 realme 1 nokia 1
Method-2: Java ArrayList forEach() Method – Example with Integer Type ArrayList
Approach:
- Create a new ArrayList of type Integer.
- Add Integer elements into the ArrayList using the add() method.
- Display the ArrayList elements.
- Now, using the
forEach()
method traverse the arraylist and perform given action for each element one by one. - Print the new arrayList.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create an ArrayList of Integer datatype ArrayList<Integer> arr1 = new ArrayList<Integer>(); // Adding some elements to the ArrayList arr1.add(1); arr1.add(100); arr1.add(84); arr1.add(17); arr1.add(0); // Prints the ArrayList elements System.out.println("The elements in the ArrayList are: "+arr1); // add 1 on each element with a space using forEach() method System.out.print("The new elements in the ArrayList are: "); arr1.forEach((e) -> {System.out.print(e +1+" ");}); } }
Output: The elements in the ArrayList are: [1, 100, 84, 17, 0] The new elements in the ArrayList are: 2 101 85 18 1
Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples.
Related Java Programs: