Stream API Flashcards
7 cards from real 1Z0-819 practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.
Read the first 7 Stream API flashcards as text
Which terminal operation must be used if you need the result to maintain encounter order in a parallel stream?
Answer: forEachOrdered()
forEachOrdered() respects the encounter order even in parallel streams, unlike forEach() which may process elements in any order.
What does Collectors.summarizingInt(ToIntFunction) produce?
Answer: An IntSummaryStatistics object
summarizingInt() collects count, sum, min, max, and average into an IntSummaryStatistics instance.
Which method creates a Stream by reading lines from a file and is defined in java.nio.file.Files?
Answer: Files.lines()
Files.lines(Path) returns a lazily-populated Stream of lines from the file.
What is the difference between Stream.anyMatch() and Stream.allMatch() when the stream is empty?
Answer: anyMatch returns false; allMatch returns true
On an empty stream, anyMatch() returns false (vacuously no element satisfies the predicate) and allMatch() returns true (vacuously all elements satisfy).
Which collector can be used to produce a Map> from a stream?
Answer: Collectors.partitioningBy()
partitioningBy(Predicate) always produces a two-entry Map> keyed on true and false.
What does the following return? Stream.of("hello","world").map(String::toUpperCase).collect(Collectors.joining("-"))
Answer: HELLO-WORLD
map(String::toUpperCase) converts each string to uppercase; joining("-") concatenates them with a hyphen delimiter.
Which of the following best describes a lazy intermediate operation in the Stream API?
Answer: It is not executed until a terminal operation is invoked
Intermediate operations like filter() and map() are lazy — they build a pipeline description but do no work until a terminal operation triggers processing.