-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcollectors-teeing.json
More file actions
50 lines (50 loc) · 1.87 KB
/
Copy pathcollectors-teeing.json
File metadata and controls
50 lines (50 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{
"id": 26,
"slug": "collectors-teeing",
"title": "Collectors.teeing()",
"category": "collections",
"difficulty": "intermediate",
"jdkVersion": "12",
"oldLabel": "Java 8",
"modernLabel": "Java 12+",
"oldApproach": "Two Passes",
"modernApproach": "teeing()",
"oldCode": "long count = items.stream().count();\ndouble sum = items.stream()\n .mapToDouble(Item::price)\n .sum();\nvar result = new Stats(count, sum);",
"modernCode": "var result = items.stream().collect(\n Collectors.teeing(\n Collectors.counting(),\n Collectors.summingDouble(Item::price),\n Stats::new\n )\n);",
"summary": "Compute two aggregations in a single stream pass.",
"explanation": "Collectors.teeing() sends each element to two downstream collectors and merges the results. This avoids streaming the data twice or using a mutable accumulator.",
"whyModernWins": [
{
"icon": "⚡",
"title": "Single pass",
"desc": "Process the stream once instead of twice."
},
{
"icon": "🧩",
"title": "Composable",
"desc": "Combine any two collectors with a merger function."
},
{
"icon": "🔒",
"title": "Immutable result",
"desc": "Merge into a record or value object directly."
}
],
"support": {
"state": "available",
"description": "Widely available since JDK 12 (March 2019)"
},
"prev": "collections/sequenced-collections",
"next": "collections/stream-toarray-typed",
"related": [
"collections/copying-collections-immutably",
"collections/unmodifiable-collectors",
"collections/stream-toarray-typed"
],
"docs": [
{
"title": "Collectors.teeing()",
"href": "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/stream/Collectors.html#teeing(java.util.stream.Collector,java.util.stream.Collector,java.util.function.BiFunction)"
}
]
}