In Java, the forEach() method is used to iterate over elements in a collection or an array and perform an action on each element. It is available for collections that implement the Iterable interface or arrays.
The forEach() method accepts a lambda expression or a method reference as its parameter. The lambda expression or method reference specifies the action to be performed on each element of the collection or array.
Here’s the general syntax for using the forEach() method:
collectionOrArray.forEach(element -> {
    <span class="hljs-comment">// Perform action on each element</span>
});
Let’s look at a few examples to see how to use the forEach() method:
Example 1: Using forEach() with a List
<span class="hljs-keyword">import</span> java.util.ArrayList;
<span class="hljs-keyword">import</span> java.util.List;
<span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Main</span> {
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> {
        List<String> fruits = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span><>();
        fruits.add(<span class="hljs-string">"Apple"</span>);
        fruits.add(<span class="hljs-string">"Banana"</span>);
        fruits.add(<span class="hljs-string">"Orange"</span>);
        <span class="hljs-comment">// Using forEach with a lambda expression</span>
        fruits.forEach(fruit -> {
            System.out.println(fruit);
        });
        <span class="hljs-comment">// Using forEach with a method reference</span>
        fruits.forEach(System.out::println);
    }
}
Output:
<span class="hljs-variable">Apple</span>
<span class="hljs-variable">Banana</span>
<span class="hljs-built_in">Orange</span>
Example 2: Using forEach() with an Array
<span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Main</span> {
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> {
        <span class="hljs-type">int</span>[] numbers = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>};
        <span class="hljs-comment">// Using forEach with a lambda expression</span>
        Arrays.stream(numbers).forEach(number -> {
            System.out.println(number);
        });
        <span class="hljs-comment">// Using forEach with a method reference</span>
        Arrays.stream(numbers).forEach(System.out::println);
    }
}
Output:
1
2
3
4
5
In both examples, the forEach() method is called on the collection or array, and the action to be performed on each element is specified using a lambda expression or a method reference.
Note that the forEach() method does not guarantee the order of element processing for collections like HashSet or HashMap, as they do not have a defined order.
