Java ArrayList forEach
Overview
In Java, the ArrayList collection has a method called forEach(), which lets us perform some operation on each element of the collection. The method traverses each element of the Iterable of ArrayList until all elements have been processed or an exception is raised.
Syntax of ArrayList forEach() in Java
Method signature:
The syntax for forEach() method is:
Parameters of ArrayList forEach() in Java
forEach() method takes in a single parameter: action, which represents the action to be performed for each element.
Consumer is a class that can be used when an object needs to be taken as input, and some operation is to be performed on the object without returning any result.
E signifies the data type of the ArrayList on which the action is being performed.
Thus the only parameter action is the operation that needs to be performed on each item in the ArrayList of data type E.
Return Values of ArrayList forEach() in Java
forEach() method does not return any value.
Exceptions of ArrayList forEach() in Java
NullPointerException: forEach throws a NullPointerException if the action passed to the method is null.
Output
Example
We mostly use lambda functions within forEach to perform a certain operation on the items in the ArrayList.
For example, consider the following operation where we are printing the elements of a list line by line.
Output
What is ArrayList forEach() in Java?
We should use forEach instead of conventional for loops, especially when we wish to use lambda functions. This is because the lambda function avoids the creation of bulky classes.
Lambda functions are similar to methods, but they do not need a name, and they can be implemented right in the body of a method.
The above code using the lambda function can be written as:
The code becomes much cleaner and more elegant to look at.
Furthermore, the forEach operator uses an Internal Iterator which manages the iteration in the background and leaves the developer to just code what is supposed to be done with the elements of the collection. The iterator is responsible for iterating through the elements one by one.
In the example above, the argument provided is a lambda expression. This means that the method only needs to know what is to be done, and how iteration has to be done is taken care of internally.
More Examples
Example 1: ArrayList forEach() in Java
Let us say we have a list of strings that we wish to concatenate to make a line.
One by one, the strings in the words list would be concatenated, resulting in the following output:
Example 2: forEach() Method on ArrayList Which Contains a List of Numbers
Let us write a simple program to double the numbers in a list of Integers:
For every number in the list, we will multiply the number by 2 and update it in the same list itself.
Output
Example 3: forEach() Method on ArrayList Which Contains list of Objects
Let us now take a fairly elaborate example where you want to create a list of objects in a retail store from a given list of items.
For every item in the store list, we create an Item object and increment a counter to store the id. These item objects are appended to a separate list.
Output
Conclusion
- forEach() is used to iterate over the element of an ArrayList.
- forEach() expects an action as an argument and applies the action to each element.
- If the action is null, then it results in a NullPointerException.
- forEach() works excellent with lambda functions by avoiding the creation of bulky classes to define methods.