15.4.1_Using Streams Flashcards
📝️What is a java.util.stream?
A java.util.stream is a sequence of data.
📝️What is a stream pipeline?
-> A stream pipeline consists of the operations that run on a stream to produce a result.
📝️ Which analogy would you use to explain stream pipeline Flow?
Factory - Foreman
- > Java serves as the foreman when working with stream pipelines.
- > Think of declaring the stream as giving instructions to the foreman.
- > The foreman sets up the stations and tells the workers what their duties will be.
- > The workers do not start until the foreman tells them to begin.
📝️ What are the three parts to a stream pipeline?
Source | Intermediate | Terminal
📝️Source: Where the stream comes from
📝️Intermediate operations: Transforms the stream into another one.
There can be as few or as many intermediate operations as you’d like. Since streams use
lazy evaluation, the intermediate operations do not run until the terminal operation runs.
📝️Terminal operation: Actually produces a result. Since streams can be used only once,
the stream is no longer valid after a terminal operation completes.
📝️ What are the Stream static methods?
empty | of+Nullable | generate | iterate** | concat | builder
⚠️ Finite Streams -> empty | of + Nullable
⚠️ Infinite Streams -> generate | iterate **
📝️ Intermediate vs Terminal operations?
Intermdiate Terminal
Required part of a useful pipeline? ❌ ✅
Can exist multiple times in a pipeline? ✅ ❌
Return type is a stream type? ✅ ❌
Executed upon method call? ❌ ✅
Stream valid after call? ✅ ❌
📝️ What are the Terminal operations?
count max | min find{Any | First} {all | any | none}Match forEach | reduce | collect
📝️ What are the Stream Terminal Operations [PITFALLS]?
🤯️⚠️📣️ allMatch - If the stream is empty true is returned and the predicate is not evaluated
🤯️⚠️📣️ forEach - It’s the only terminal operation with a return type of void.
📝️ What are the Intermediate Operations?
filter | limit | sorted | peek
map | flatMap
distinct | skip