Loading…
Loading…
Topic
Articles and problems tagged Streams and Functional Java.
Use Java parallel streams correctly: parallelStream and parallel, why shared mutable state causes race conditions, encounter order and unordered(), groupingByConcurrent, when parallelism pays off, the common ForkJoinPool, running in a custom pool, and random numbers in parallel.
Avoid boxing with Java's IntStream, LongStream and DoubleStream. Create them with of, range, chars, codePoints and random generators, convert with mapToInt and boxed, and use sum, average, max, summaryStatistics and OptionalInt, plus the overflow and floating-point details.
Write your own Java stream gatherers: the initializer, integrator, combiner and finisher, pushing elements downstream, Gatherer.of versus ofSequential, greedy versus short-circuiting integrators, finishers that flush state, and the rules for hand-written Gatherer classes.
Use Java 24's stream gatherers, the extension point for intermediate operations. Group elements with windowFixed and windowSliding, compare neighbors, fold and scan sequentially, run blocking calls on virtual threads with mapConcurrent, and chain gatherers with andThen.
Master Java's Stream.reduce in its three forms: reduce with an Optional result, with an identity value, and with an accumulator and combiner. Learn why the operation must be associative, what a wrong identity does in parallel, and when to use the three-argument collect instead.
Learn how Java collectors work inside: the supplier, accumulator, combiner and finisher functions, the IDENTITY_FINISH, UNORDERED and CONCURRENT characteristics, building collectors with Collector.of, wrapping a downstream collector and the parallel bugs a bad combiner causes.
Process the groups of a Java groupingBy with downstream collectors: counting, summingInt, averagingInt, maxBy, mapping, joining, filtering, flatMapping, collectingAndThen, summarizingInt, reducing, nested groupingBy and teeing for two results in one pass.
Group Java stream elements with Collectors.groupingBy and split them in two with partitioningBy. Covers classifier functions, choosing TreeMap or EnumMap, why partitioningBy always has both keys, the null-key error and groupingByConcurrent for parallel streams.
Go deeper with Java lambdas: closures and effectively final variables, this inside a lambda, writing methods that accept lambdas, the complete java.util.function tables, combining predicates and functions, String.transform, and building comparators with comparing, thenComparing and nullsFirst.
Learn Java lambda expressions from scratch: why lambdas exist, every syntax form (inferred types, block bodies, no parameters, var, unnamed _), functional interfaces and @FunctionalInterface, java.util.function types like Predicate, Supplier and BiFunction, and primitive specializations.
Turn a Java stream into a map with Collectors.toMap: key and value functions, Function.identity(), the duplicate-key IllegalStateException, merge functions, choosing TreeMap or LinkedHashMap, the null-value trap, toUnmodifiableMap and toConcurrentMap.
Get results out of a Java stream: iterator, forEach versus forEachOrdered, typed arrays with toArray(String[]::new), the differences between toList(), Collectors.toList() and toUnmodifiableList(), toSet, toCollection, joining and summarizingInt statistics.
How not to use Java Optional (get, isPresent, Optional fields and parameters), how to create Optionals with of, ofNullable and empty, how to chain Optional-returning methods with flatMap, and how Optional.stream drops missing values in a stream pipeline.
Use Java's Optional the right way: unwrap with orElse, orElseGet and orElseThrow (and why orElse evaluates eagerly), consume with ifPresent and ifPresentOrElse, and transform with map, filter and or, without ever calling get.
Get answers out of Java streams with the simple terminal reductions count, max, min, findFirst, findAny, anyMatch, allMatch and noneMatch. Learn why they return Optional, how short-circuiting works, the empty-stream rules and when findAny beats findFirst.
Cut, combine and reorder Java streams: limit and skip (with paging), takeWhile and dropWhile versus filter, Stream.concat, distinct, stable sorted with comparators, and peek for debugging, including why peek can silently not run with count().
The core Java stream transformations: filter keeps elements, map transforms them, flatMap flattens nested results and mapMulti pushes zero or more results without inner streams. Includes grapheme clusters, dropping nulls with ofNullable, and the primitive mapToInt and flatMapToInt.
Every way to get a Java stream: from collections, arrays and values, infinite streams with generate and iterate, ofNullable, String.lines, Pattern, Scanner, Files.lines, Stream.Builder and iterators, plus the noninterference rule and the Stream.of(int[]) trap.
Why Java streams exist and how they differ from collections and loops: the "what, not how" style, lazy evaluation, the three stages of a pipeline (source, intermediate, terminal), single-use streams and a first look at parallel streams.