1/44
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Stream
Not a data structure, it doesn’t store data. Instead, it’s a pipeline for processing data.
Takes input like collection, array, and etc.
Applies operations like filter, map, and etc.
Produces output like result or side effect
A stream
No Storage
Functional Style
Lazy Evaluation
Characteristics of java stream.
No Storage
The stream doesn’t copy the list, it just references it.
Functional Style
Instead of how to do it, you describe what to do.
Lazy Evaluation
Nothing runs until a terminal operation is called.
From Collections
Primitive Streams
Infinite Streams
Different ways in creating steam
From Collections
Most common use-case.
List<String> names = List.of("Alice", "Bob");
names.stream();
Sample :
List<String> upperNames = names.stream() // create stream
.map(String::toUpperCase) // intermediate operation
.collect(Collectors.toList()); // terminal operation
System.out.println(upperNames);
output: [ALICE, BOB]Example of From Collection.
Boxing
Converting primitive to wrapper object.
Unboxing
Converting wrapper object to primitive.
Infinite Streams
This don’t end, unless you limit them
.filter()
An intermediate operation in Java Streams that keeps only the elements that match a given condition.
filter()
Keeps elements that match a condition.
.map()
An intermediate operation in Java Streams that transforms each element of the stream into a new form.
flatMap()
An intermediate operation in Java Streams that flattens nested structures into a single stream.
forEach()
Consumes the stream.
collect()
Converts stream into a collection.
Creates container
Adds element
Merges results that is important for parallel
Process of the collect().
Reduce()
Combines elements into one value.
groupingBy()
It is a Collector that groups elements of a stream into a Map based on a key. Groups elements into a Map.
joining()
Combines strings efficiently (better than +)
Handle mutable containers safely
Work with parallel streams
Why Collectors Matter?
parallelStream()
Splits work across multiple threads.
List<Integer> result = new ArrayList<>();
parallelStream().forEach(result::add);Avoid this when using parallelStream() because it is not thread-safe
Each thread gets its own container
Then merges safely
Why collect() Works?
Optional<T>Streams often return thus and its value may be present OR absent.
employees.stream()
.filter(e -> e.salary > 50000)Remove unwanted data early (performance benefit)
.sorted(...)Sorting is expensive → done after filtering
.map(e -> e.name)Transform to required output
.collect(...)Final result
Source
Intermediate
Terminal
Pipeline stages
list.stream()Source
.filter().map()Intermediate
.collect()Terminal
Stream Reuse
Side Effects
Overusing Streams
Common Pitfalls
Stream Reuse
Streams are single-use
Side Effects
Breaks functional model
Overusing Streams
Streams are not always clearer
Supplier
Accumulator
Combiner
Custom Collector
Supplier
Creates container
Accumulator
Adds elements
Combiner
Merges results
peek()
used for Debugging only and Should not modify data.
.peek(System.out::println)peek()