
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Difference between Collection.stream().forEach() and Collection.forEach() in Java
Collection.stream().forEach() and Collection.forEach() both are used to iterate over collection.
Collection.forEach() uses the collection’s iterator. Most of the collections doesn’t allow the structurally modification while iterating over them. If any element add or remove while iteration they will immediately throw concurrent modification exception. If Collection.forEach() is iterating over the synchronized collection then they will lock the segment of the collection and hold it across all the calls.
Collection.stream().forEach() is also used for iterating the collection but it first convert the collection to the stream and then iterate over the stream of the collection therefore the processing order is undefined. It also throws the concurrent modification exception, if any structural changes happened while iterating over them it will throw an exception immediately.
Sr. No. | Key | Collection.forEach() | Collection.stream().forEach() |
---|---|---|---|
1 | Basic | Collection.forEach() uses the collection’s iterator | Collection.stream().forEach() is also used for iterating the collection but it first convert the collection to the stream and then iterate over the stream of the collection |
2 | Order | It always executed in the iteration order of the Iterable, if one is specified. | Order is not defined |
3 | Lock | If iteration is happening over the synchronized collection then It lock the collection and hold it across all the calls | It does not lock the collection |
4. | Exception | It will immediately throw an exception in the case of any structural modification happened in the collection | Exception will be thrown later |
Example Collection.stream().forEach
import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List list= new ArrayList(); list.add("Ram"); list.add("TutorialPoints"); list.stream().forEach(System.out::print); } }
Example Collection.forEach
import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List list= new ArrayList(); list.add("Ram"); list.add("TutorialPoints"); list.forEach(System.out::print); } }
- Related Articles
- Difference Between For and Foreach in PHP
- What is the difference between Foreach and Parallel.Foreach in C#?
- What is difference between forEach() and map() method in JavaScript?
- Foreach in C++ and Java
- foreach in Java
- Iterator vs forEach in Java
- IntStream forEach() method in Java
- LongStream forEach() method in Java
- DoubleStream forEach() method in Java
- Foreach in C++ vs Java
- Difference between Java and JavaScript.
- Difference between Go and Java.
- Difference Between C++ and Java
- Difference between Groovy and Java
- Difference between constructor and method in Java
