Streams
Turn loops into readable pipelines: filter, map, collect, and parallel work.
Take this course in the learning hubLessons
- Why Streams ExistWhen a stream beats a for loop, and what a stream actually is.
- Creating a StreamEvery common way to get a Stream, including infinite ones and how to bound them.
- Pipeline AnatomyThe three parts of every pipeline, and why nothing runs without a terminal op.
- filter and mapKeep what you want with filter, reshape it with map, and chain the two safely.
- flatMap: Flattening Nested DataTurn nested collections into one flat stream, and expand one element into many.
- sorted, distinct, limit, skipOrder, de-duplicate and page a stream, and know which of these ops cost memory.
- reduce: Many Values Into OneFold a whole stream into a single value, with or without a starting identity.
- collect and the Basic CollectorsPour a stream into a List, Set, Map or String using collect and Collectors.
- groupingBy and partitioningByBuild grouped reports in one pass with groupingBy, partitioningBy and downstream collectors.
- teeing and Summary StatisticsGet two answers from a single pass with teeing, plus full number summaries.
- Primitive Streams (IntStream)Work with numbers without boxing, using IntStream, LongStream and DoubleStream.
- Optional and StreamsHandle the maybe-nothing results streams hand back, and flatten Optionals away.
- Laziness and Short-CircuitingWhy streams do the minimum work possible, and how that makes infinite sources safe.
- Parallel StreamsWhen parallel streams genuinely speed things up, and when they quietly make it worse.
- Common Stream MistakesThe stream traps that catch everyone, and the fix for each of them.
- peek: Watching a Pipeline RunSee what each stage receives without changing the data, and learn where peek lies to you.
- anyMatch, allMatch and noneMatchAsk a yes or no question about a stream and stop reading the moment the answer is known.
- findFirst vs findAnyBoth return an Optional match. Only one of them promises you the first element in order.
- iterate and generate: Infinite StreamsBuild a stream that never ends, then bound it so the program still finishes.
- takeWhile and dropWhileCut a stream at the first element that fails the test, instead of testing every element.
- toArray and Back AgainGet a real array out of a stream, keep the element type, and stream an array back in.
- Joining Strings from a StreamBuild a separated, wrapped string in one pass, with no StringBuilder and no trailing comma.
- Writing a Custom CollectorThe four functions behind every collector, and how to assemble your own when none of them fit.
- mapMulti: flatMap Without the StreamsPush zero, one or many results downstream per element, with no inner Stream allocated.
- Streaming Text: lines, chars, splitTurn a document into lines, lines into words and words into counts, all as streams.
- Method References in DepthThe four shapes of a method reference, what each one expands to, and when a lambda is clearer.
- Streaming a MapFilter, sort and rebuild maps through entrySet, and keep the order you asked for.
- When a Plain Loop WinsThe four jobs where a for loop is still the clearer, faster and more honest answer.
- Lambdas and Functional InterfacesThe handful of interface shapes every stream op takes, and how a lambda becomes one.
- Composing Predicates and FunctionsBuild one lambda from several with and, or, negate, andThen and compose.
- Comparators in DepthSort by one key, break ties with another, reverse it, and survive a null.
- Downstream Collectorsfiltering, flatMapping, reducing and collectingAndThen: the second tier of Collectors.
- Nulls in a StreamWhere a null actually blows up in a pipeline, and the cheapest place to remove it.
- Checked Exceptions in LambdasWhy a lambda cannot throw a checked exception, and the three honest ways around it.
- Encounter Order and unordered()Which sources have an order, which ops must respect it, and what that costs in parallel.
- Spliterators: How a Stream SplitsWhat parallel() actually does, and why the source decides whether you get a speed-up.
- Streams That Hold a ResourceFiles.lines opens a real file handle. Close it, or you leak one on every call.
- Refactoring a Loop into a PipelineA mechanical recipe for turning a nested report loop into one readable pipeline.
- What parallel() actually doesOne shared pool, a splitting cost, and a source that may not split at all.
- Stateful lambdas and side effectsCode that is correct sequentially and silently wrong in parallel.
- Writing a Collector properlySupplier, accumulator, combiner, finisher - and what the characteristics promise.
- When a stream is the wrong toolBoxing, megamorphic call sites, and the loop that is genuinely clearer.