A Collection stores data. A Stream processes data.
List,Set, andMapkeep elements in memory- A
Streamreads those elements and applies operations to them - A stream does not change the original collection unless you store the result somewhere
Example
var numbers = List.of(1, 2, 3, 4, 5);
var evenNumbers = numbers.stream()
.filter(number -> number % 2 == 0)
.toList();
System.out.println(numbers);
System.out.println(evenNumbers);Output
[1, 2, 3, 4, 5]
[2, 4]| Previous: E Structure Of A Stream Pipeline.md | Next: G Intermediate Vs Terminal Operations.md |