Class StreamEnhancementExamples
java.lang.Object
net.jrodolfo.java_evolution.java09.StreamEnhancementExamples
Demonstrates Stream API enhancements introduced in Java 9.
Java 8 streams made pipeline-style data processing practical, but some common stream shapes still required custom logic: process ordered values until a boundary, skip ordered values until a boundary, safely stream a possibly null value, or generate values with a built-in stopping condition.
Java 9 added ordered stream slicing with takeWhile and
dropWhile, nullable stream creation with Stream.ofNullable,
and a bounded form of Stream.iterate.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionlistFromNullableValue(String value) UsesStream.ofNullable(Object)to create either a one-element stream or an empty stream.numbersBeforeTen(List<Integer> numbers) UsesStream.takeWhile(java.util.function.Predicate)to keep values only until the first value that does not match.numbersFromTenOnward(List<Integer> numbers) UsesStream.dropWhile(java.util.function.Predicate)to skip values until the first value that does not match.Uses the Java 9 bounded form ofStream.iterate(Object, java.util.function.Predicate, java.util.function.UnaryOperator).
-
Constructor Details
-
StreamEnhancementExamples
public StreamEnhancementExamples()
-
-
Method Details
-
numbersBeforeTen
UsesStream.takeWhile(java.util.function.Predicate)to keep values only until the first value that does not match.- Parameters:
numbers- ordered numbers to inspect- Returns:
- the numbers before the first value greater than or equal to 10
-
numbersFromTenOnward
UsesStream.dropWhile(java.util.function.Predicate)to skip values until the first value that does not match.- Parameters:
numbers- ordered numbers to inspect- Returns:
- the numbers starting at the first value greater than or equal to 10
-
listFromNullableValue
UsesStream.ofNullable(Object)to create either a one-element stream or an empty stream.- Parameters:
value- the possibly null value- Returns:
- a list containing the value when non-null, otherwise an empty list
-
powersOfTwoBelowOneHundred
Uses the Java 9 bounded form ofStream.iterate(Object, java.util.function.Predicate, java.util.function.UnaryOperator).- Returns:
- powers of two that are smaller than 100
-