Skip to content

Commit 5dd3132

Browse files
fix: reinforce teeing and reduce classification guidance
1 parent b9a97ad commit 5dd3132

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

skills/java-streams/SKILL.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ are equivalent.
3939
- concatenated text: `Collectors.joining`;
4040
- numeric primitive result: `mapToInt`/`mapToLong`/`mapToDouble` plus primitive stream terminal
4141
operations;
42+
- two independent aggregates over the same input on Java 12+: `Collectors.teeing`;
4243
- grouping/indexing: `groupingBy`, downstream collectors, `partitioningBy`, or `toMap` with
4344
explicit merge/null handling.
4445
2. Prefer stream terminal operations that encode intent directly: `anyMatch` for existence, `count`
@@ -48,8 +49,8 @@ are equivalent.
4849
`flatMap(Optional::stream)` on Java 9+ for `Stream<Optional<T>>`. On Java 16+, consider
4950
`mapMulti` only when it makes a small zero-or-one/one-to-few transformation clearer or avoids
5051
many tiny stream allocations.
51-
4. Use primitive streams for primitive aggregation. Keep `reduce(identity, op)` for immutable
52-
non-primitive accumulation such as `BigDecimal`.
52+
4. Use primitive streams for primitive aggregation. Keep and explicitly classify `reduce(identity,
53+
op)` as acceptable for immutable non-primitive accumulation such as `BigDecimal`.
5354
5. Choose collectors by result semantics, and state duplicate-key/null contracts explicitly. When a
5455
later step needs an expensive check result, carry `element + result` with a baseline-compatible
5556
holder; use `Map.entry` only on Java 9+ when both values are non-null.

skills/java-streams/references/hard-stops.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Fix these before finalizing:
2121
- `map(...).collect(toList())` followed immediately by `String.join`. Use `Collectors.joining`.
2222
- Boxed numeric `reduce` for primitive totals/statistics. Use primitive streams or summarizing
2323
collectors unless the type is genuinely non-primitive, such as `BigDecimal`.
24+
In audits, explicitly classify non-primitive reductions such as
25+
`reduce(BigDecimal.ZERO, BigDecimal::add)` as acceptable.
2426
- Nested `map(... stream ... collect(...)).flatMap(...)` where a direct `flatMap` pipeline is
2527
clearer.
2628
- `filter(Optional::isPresent).map(Optional::get)` on Java 9+. Use `flatMap(Optional::stream)`.

skills/java-streams/references/stream-examples.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,14 @@ List<Product> favoriteProducts = user.getFavoriteProducts().stream()
209209
`Map.entry` is appropriate in this example because the baseline is Java 24 and neither side of the
210210
entry is null. If nulls can reach the carrier or the baseline is Java 8, use a null-tolerant project
211211
type or `AbstractMap.SimpleImmutableEntry`.
212+
213+
Use Java 12+ `Collectors.teeing` when two independent aggregates should be computed over the same
214+
input:
215+
216+
```java
217+
Pair<Product, Product> range = products.stream()
218+
.collect(Collectors.teeing(
219+
Collectors.minBy(Comparator.comparing(Product::price)),
220+
Collectors.maxBy(Comparator.comparing(Product::price)),
221+
(min, max) -> new Pair<>(min.orElse(null), max.orElse(null))));
222+
```

0 commit comments

Comments
 (0)