Sub topic 2
What Is a Stream?
A stream is a sequence of elements that you process step by step. You get a stream from a data source, such as a List. Then you tell Java what to do with each element. A stream does not store anything itself. It carries elements from the source through your steps to a result.
Streams arrived in Java 8. They let you describe what you want, such as "keep the scores of 50 or more." You no longer have to spell out how to loop over the data.
A Collection Versus a Stream
A collection is an object that holds data in memory. List, Set, and Map are collections. You can add to a collection, remove from it, and read it as many times as you like.
A stream works more like a conveyor belt. Elements pass along it one at a time. Each element goes through your steps, and then the belt is finished.
| Feature | Collection | Stream |
|---|---|---|
| Stores elements | Yes | No |
| Can be read many times | Yes | No, only once |
| Changes the source | It can (add, remove) | Never |
| Main job | Hold data | Compute a result from data |
Loop Versus Stream: A Worked Example
Say you have some test scores and want the passing ones (50 or more). Here is the same task written with a loop and then with a stream.
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class LoopVsStream {
public static void main(String[] args) {
List<Integer> scores = List.of(45, 82, 67, 91, 30);
// Loop style: you manage the new list and the if check
List<Integer> passedLoop = new ArrayList<>();
for (int s : scores) {
if (s >= 50) {
passedLoop.add(s);
}
}
// Stream style: you describe the result you want
List<Integer> passedStream = scores.stream()
.filter(s -> s >= 50)
.collect(Collectors.toList());
System.out.println(passedLoop); // [82, 67, 91]
System.out.println(passedStream); // [82, 67, 91]
System.out.println(scores); // [45, 82, 67, 91, 30]
}
}Here is what each line of the stream version does:
scores.stream()opens a stream over the list..filter(s -> s >= 50)keeps only the elements that pass the test..collect(Collectors.toList())gathers what is left into a new list.
The last line prints the original scores list. It is unchanged.
Why Use Streams?
- Readable: each step names its job, such as
filter. - Fewer bugs: you do not manage counters or temporary lists by hand.
- Easy to chain: you can add steps one after another, like links in a chain.
Later chapters cover how these steps fit together and which steps you can use.
Exercise
You run names.stream().filter(n -> n.startsWith("A")).collect(Collectors.toList()) on a list of five names, and two of them start with "A". How many names are in the original names list afterward, and why?
Show answer
Five. A stream never changes its source. The filter builds a new list with the two matching names, and the original list stays as it was.
Exercise
Name two ways a stream differs from a collection.
Show answer
A stream does not store elements, and it can be used only once. It also never changes its source, while you can add to or remove from a collection.