Open In App

Difference Between Collection.stream().forEach() and Collection.forEach() in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Collection.forEach() and Collection.stream().forEach() are used for iterating over the collections, there is no such major difference between the two, as both of them give the same result, though there are some differences in their internal working.

Collection.stream().forEach() is basically used for iteration in a group of objects by converting a collection into the stream and then iterate over the stream of collection. While iterating over the collection, if any structural changes are made to collections, then it will throw the concurrent modification exception.

Collection.forEach() uses the collection’s iterator(whichever is specified). The majority of the collections do not allow modifications in the structure while iterating over that collection. If any changes happen to that collection, i.e. addition of an element or the removal of the element, then they will throw the concurrent modification error. If collection.forEach() is iterating over a synchronized collection, then they will lock the segment of collection and hold it over all the calls that can be made w.r.t to that collection.

Difference Between Collection.stream().forEach() and Collection.forEach()

  Collection.stream().forEach()

                                    Collection.forEach()
Collection.stream().forEach() is also used for iterating the collection but it first converts the collection to the stream and then iterates over the stream of collection. Collection.forEach() uses the collections iterator.
Unlike Collection.forEach() it does not execute in any specific order, i.e. the order is not defined. If always execute in the iteration order of iterable, if one is specified.
During structure modification in the collection, the exception will be thrown later. If we perform any structural modification in the collection by using the collection.forEach() it will immediately throw an exception.
If iteration is happening over the synchronized collection, then it does not lock the collection. If iteration is happening over the synchronized collection, then it locks the collection and holds it across all the calls.

Below is a Java program to show the usage of the Collection.stream.forEach():

Java




// Java Program to show the demonstration of
// Collection.stream().forEach()
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        List<Integer> list = new ArrayList<>();
  
        list.add(5);
        list.add(6);
        list.add(3);
        list.add(4);
        // printing each element of list using forEach loop
        list.stream().forEach(System.out::print);
    }
}


Output

5634

Below is a Java program to show the usage of Collection.forEach() Method:

Java




// Java Program to show the demonstration of
// Collection.forEach()
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        List<Integer> list = new ArrayList<>();
  
        list.add(5);
        list.add(6);
        list.add(3);
        list.add(4);
        // printing each element of list
        list.forEach(System.out::print);
    }
}


Output

5634


Last Updated : 02 Feb, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads