Skip to content

Latest commit

 

History

History
37 lines (30 loc) · 876 Bytes

File metadata and controls

37 lines (30 loc) · 876 Bytes

Stream vs Collection

A Collection stores data. A Stream processes data.

  • List, Set, and Map keep elements in memory
  • A Stream reads 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