author | zmajo |
Fri, 03 Jul 2015 07:23:45 +0200 | |
changeset 31671 | 362e0c0acece |
parent 29218 | a2c7a365dde4 |
child 32755 | 6e8998981fbd |
permissions | -rw-r--r-- |
17196 | 1 |
/* |
29218 | 2 |
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. |
17196 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
package java.util.stream; |
|
26 |
||
27 |
import java.util.AbstractMap; |
|
28 |
import java.util.AbstractSet; |
|
29 |
import java.util.ArrayList; |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
30 |
import java.util.Arrays; |
17196 | 31 |
import java.util.Collection; |
32 |
import java.util.Collections; |
|
33 |
import java.util.Comparator; |
|
34 |
import java.util.DoubleSummaryStatistics; |
|
35 |
import java.util.EnumSet; |
|
36 |
import java.util.HashMap; |
|
37 |
import java.util.HashSet; |
|
38 |
import java.util.IntSummaryStatistics; |
|
39 |
import java.util.Iterator; |
|
40 |
import java.util.List; |
|
41 |
import java.util.LongSummaryStatistics; |
|
42 |
import java.util.Map; |
|
43 |
import java.util.Objects; |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
44 |
import java.util.Optional; |
17196 | 45 |
import java.util.Set; |
46 |
import java.util.StringJoiner; |
|
47 |
import java.util.concurrent.ConcurrentHashMap; |
|
48 |
import java.util.concurrent.ConcurrentMap; |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
49 |
import java.util.function.BiConsumer; |
17196 | 50 |
import java.util.function.BiFunction; |
51 |
import java.util.function.BinaryOperator; |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
52 |
import java.util.function.Consumer; |
17196 | 53 |
import java.util.function.Function; |
54 |
import java.util.function.Predicate; |
|
55 |
import java.util.function.Supplier; |
|
56 |
import java.util.function.ToDoubleFunction; |
|
57 |
import java.util.function.ToIntFunction; |
|
58 |
import java.util.function.ToLongFunction; |
|
59 |
||
60 |
/** |
|
61 |
* Implementations of {@link Collector} that implement various useful reduction |
|
62 |
* operations, such as accumulating elements into collections, summarizing |
|
63 |
* elements according to various criteria, etc. |
|
64 |
* |
|
19850 | 65 |
* <p>The following are examples of using the predefined collectors to perform |
66 |
* common mutable reduction tasks: |
|
17196 | 67 |
* |
68 |
* <pre>{@code |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
69 |
* // Accumulate names into a List |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
70 |
* List<String> list = people.stream().map(Person::getName).collect(Collectors.toList()); |
17196 | 71 |
* |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
72 |
* // Accumulate names into a TreeSet |
19850 | 73 |
* Set<String> set = people.stream().map(Person::getName).collect(Collectors.toCollection(TreeSet::new)); |
17196 | 74 |
* |
75 |
* // Convert elements to strings and concatenate them, separated by commas |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
76 |
* String joined = things.stream() |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
77 |
* .map(Object::toString) |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
78 |
* .collect(Collectors.joining(", ")); |
17196 | 79 |
* |
19850 | 80 |
* // Compute sum of salaries of employee |
81 |
* int total = employees.stream() |
|
82 |
* .collect(Collectors.summingInt(Employee::getSalary))); |
|
17196 | 83 |
* |
84 |
* // Group employees by department |
|
85 |
* Map<Department, List<Employee>> byDept |
|
86 |
* = employees.stream() |
|
87 |
* .collect(Collectors.groupingBy(Employee::getDepartment)); |
|
88 |
* |
|
19850 | 89 |
* // Compute sum of salaries by department |
90 |
* Map<Department, Integer> totalByDept |
|
17196 | 91 |
* = employees.stream() |
92 |
* .collect(Collectors.groupingBy(Employee::getDepartment, |
|
19850 | 93 |
* Collectors.summingInt(Employee::getSalary))); |
17196 | 94 |
* |
95 |
* // Partition students into passing and failing |
|
96 |
* Map<Boolean, List<Student>> passingFailing = |
|
97 |
* students.stream() |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
98 |
* .collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD)); |
17196 | 99 |
* |
100 |
* }</pre> |
|
101 |
* |
|
102 |
* @since 1.8 |
|
103 |
*/ |
|
104 |
public final class Collectors { |
|
105 |
||
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
106 |
static final Set<Collector.Characteristics> CH_CONCURRENT_ID |
17196 | 107 |
= Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.CONCURRENT, |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
108 |
Collector.Characteristics.UNORDERED, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
109 |
Collector.Characteristics.IDENTITY_FINISH)); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
110 |
static final Set<Collector.Characteristics> CH_CONCURRENT_NOID |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
111 |
= Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.CONCURRENT, |
17196 | 112 |
Collector.Characteristics.UNORDERED)); |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
113 |
static final Set<Collector.Characteristics> CH_ID |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
114 |
= Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.IDENTITY_FINISH)); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
115 |
static final Set<Collector.Characteristics> CH_UNORDERED_ID |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
116 |
= Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.UNORDERED, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
117 |
Collector.Characteristics.IDENTITY_FINISH)); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
118 |
static final Set<Collector.Characteristics> CH_NOID = Collections.emptySet(); |
17196 | 119 |
|
120 |
private Collectors() { } |
|
121 |
||
122 |
/** |
|
24123
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
123 |
* Construct an {@code IllegalStateException} with appropriate message. |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
124 |
* |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
125 |
* @param k the duplicate key |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
126 |
* @param u 1st value to be accumulated/merged |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
127 |
* @param v 2nd value to be accumulated/merged |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
128 |
*/ |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
129 |
private static IllegalStateException duplicateKeyException( |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
130 |
Object k, Object u, Object v) { |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
131 |
return new IllegalStateException(String.format( |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
132 |
"Duplicate key %s (attempted merging values %s and %s)", |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
133 |
k, u, v)); |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
134 |
} |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
135 |
|
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
136 |
/** |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
137 |
* {@code BinaryOperator<Map>} that merges the contents of its right |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
138 |
* argument into its left argument, throwing {@code IllegalStateException} |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
139 |
* if duplicate keys are encountered. |
17196 | 140 |
* |
24123
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
141 |
* @param <K> type of the map keys |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
142 |
* @param <V> type of the map values |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
143 |
* @param <M> type of the map |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
144 |
* @return a merge function for two maps |
17196 | 145 |
*/ |
24123
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
146 |
private static <K, V, M extends Map<K,V>> |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
147 |
BinaryOperator<M> uniqKeysMapMerger() { |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
148 |
return (m1, m2) -> { |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
149 |
for (Map.Entry<K,V> e : m2.entrySet()) { |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
150 |
K k = e.getKey(); |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
151 |
V v = Objects.requireNonNull(e.getValue()); |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
152 |
V u = m1.putIfAbsent(k, v); |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
153 |
if (u != null) throw duplicateKeyException(k, u, v); |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
154 |
} |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
155 |
return m1; |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
156 |
}; |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
157 |
} |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
158 |
|
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
159 |
/** |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
160 |
* {@code BiConsumer<Map, T>} that accumulates (key, value) pairs |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
161 |
* extracted from elements into the map, throwing {@code IllegalStateException} |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
162 |
* if duplicate keys are encountered. |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
163 |
* |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
164 |
* @param keyMapper a function that maps an element into a key |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
165 |
* @param valueMapper a function that maps an element into a value |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
166 |
* @param <T> type of elements |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
167 |
* @param <K> type of map keys |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
168 |
* @param <V> type of map values |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
169 |
* @return an accumulating consumer |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
170 |
*/ |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
171 |
private static <T, K, V> |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
172 |
BiConsumer<Map<K, V>, T> uniqKeysMapAccumulator(Function<? super T, ? extends K> keyMapper, |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
173 |
Function<? super T, ? extends V> valueMapper) { |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
174 |
return (map, element) -> { |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
175 |
K k = keyMapper.apply(element); |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
176 |
V v = Objects.requireNonNull(valueMapper.apply(element)); |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
177 |
V u = map.putIfAbsent(k, v); |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
178 |
if (u != null) throw duplicateKeyException(k, u, v); |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
179 |
}; |
17196 | 180 |
} |
181 |
||
19593 | 182 |
@SuppressWarnings("unchecked") |
183 |
private static <I, R> Function<I, R> castingIdentity() { |
|
184 |
return i -> (R) i; |
|
185 |
} |
|
186 |
||
17196 | 187 |
/** |
188 |
* Simple implementation class for {@code Collector}. |
|
189 |
* |
|
190 |
* @param <T> the type of elements to be collected |
|
191 |
* @param <R> the type of the result |
|
192 |
*/ |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
193 |
static class CollectorImpl<T, A, R> implements Collector<T, A, R> { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
194 |
private final Supplier<A> supplier; |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
195 |
private final BiConsumer<A, T> accumulator; |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
196 |
private final BinaryOperator<A> combiner; |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
197 |
private final Function<A, R> finisher; |
17196 | 198 |
private final Set<Characteristics> characteristics; |
199 |
||
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
200 |
CollectorImpl(Supplier<A> supplier, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
201 |
BiConsumer<A, T> accumulator, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
202 |
BinaryOperator<A> combiner, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
203 |
Function<A,R> finisher, |
17196 | 204 |
Set<Characteristics> characteristics) { |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
205 |
this.supplier = supplier; |
17196 | 206 |
this.accumulator = accumulator; |
207 |
this.combiner = combiner; |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
208 |
this.finisher = finisher; |
17196 | 209 |
this.characteristics = characteristics; |
210 |
} |
|
211 |
||
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
212 |
CollectorImpl(Supplier<A> supplier, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
213 |
BiConsumer<A, T> accumulator, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
214 |
BinaryOperator<A> combiner, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
215 |
Set<Characteristics> characteristics) { |
19593 | 216 |
this(supplier, accumulator, combiner, castingIdentity(), characteristics); |
17196 | 217 |
} |
218 |
||
219 |
@Override |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
220 |
public BiConsumer<A, T> accumulator() { |
17196 | 221 |
return accumulator; |
222 |
} |
|
223 |
||
224 |
@Override |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
225 |
public Supplier<A> supplier() { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
226 |
return supplier; |
17196 | 227 |
} |
228 |
||
229 |
@Override |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
230 |
public BinaryOperator<A> combiner() { |
17196 | 231 |
return combiner; |
232 |
} |
|
233 |
||
234 |
@Override |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
235 |
public Function<A, R> finisher() { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
236 |
return finisher; |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
237 |
} |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
238 |
|
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
239 |
@Override |
17196 | 240 |
public Set<Characteristics> characteristics() { |
241 |
return characteristics; |
|
242 |
} |
|
243 |
} |
|
244 |
||
245 |
/** |
|
246 |
* Returns a {@code Collector} that accumulates the input elements into a |
|
247 |
* new {@code Collection}, in encounter order. The {@code Collection} is |
|
248 |
* created by the provided factory. |
|
249 |
* |
|
250 |
* @param <T> the type of the input elements |
|
251 |
* @param <C> the type of the resulting {@code Collection} |
|
252 |
* @param collectionFactory a {@code Supplier} which returns a new, empty |
|
253 |
* {@code Collection} of the appropriate type |
|
254 |
* @return a {@code Collector} which collects all the input elements into a |
|
255 |
* {@code Collection}, in encounter order |
|
256 |
*/ |
|
257 |
public static <T, C extends Collection<T>> |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
258 |
Collector<T, ?, C> toCollection(Supplier<C> collectionFactory) { |
19593 | 259 |
return new CollectorImpl<>(collectionFactory, Collection<T>::add, |
17196 | 260 |
(r1, r2) -> { r1.addAll(r2); return r1; }, |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
261 |
CH_ID); |
17196 | 262 |
} |
263 |
||
264 |
/** |
|
265 |
* Returns a {@code Collector} that accumulates the input elements into a |
|
266 |
* new {@code List}. There are no guarantees on the type, mutability, |
|
19850 | 267 |
* serializability, or thread-safety of the {@code List} returned; if more |
268 |
* control over the returned {@code List} is required, use {@link #toCollection(Supplier)}. |
|
17196 | 269 |
* |
270 |
* @param <T> the type of the input elements |
|
271 |
* @return a {@code Collector} which collects all the input elements into a |
|
272 |
* {@code List}, in encounter order |
|
273 |
*/ |
|
274 |
public static <T> |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
275 |
Collector<T, ?, List<T>> toList() { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
276 |
return new CollectorImpl<>((Supplier<List<T>>) ArrayList::new, List::add, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
277 |
(left, right) -> { left.addAll(right); return left; }, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
278 |
CH_ID); |
17196 | 279 |
} |
280 |
||
281 |
/** |
|
282 |
* Returns a {@code Collector} that accumulates the input elements into a |
|
283 |
* new {@code Set}. There are no guarantees on the type, mutability, |
|
19850 | 284 |
* serializability, or thread-safety of the {@code Set} returned; if more |
285 |
* control over the returned {@code Set} is required, use |
|
286 |
* {@link #toCollection(Supplier)}. |
|
17196 | 287 |
* |
288 |
* <p>This is an {@link Collector.Characteristics#UNORDERED unordered} |
|
289 |
* Collector. |
|
290 |
* |
|
291 |
* @param <T> the type of the input elements |
|
292 |
* @return a {@code Collector} which collects all the input elements into a |
|
293 |
* {@code Set} |
|
294 |
*/ |
|
295 |
public static <T> |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
296 |
Collector<T, ?, Set<T>> toSet() { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
297 |
return new CollectorImpl<>((Supplier<Set<T>>) HashSet::new, Set::add, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
298 |
(left, right) -> { left.addAll(right); return left; }, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
299 |
CH_UNORDERED_ID); |
17196 | 300 |
} |
301 |
||
302 |
/** |
|
303 |
* Returns a {@code Collector} that concatenates the input elements into a |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
304 |
* {@code String}, in encounter order. |
17196 | 305 |
* |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
306 |
* @return a {@code Collector} that concatenates the input elements into a |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
307 |
* {@code String}, in encounter order |
17196 | 308 |
*/ |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
309 |
public static Collector<CharSequence, ?, String> joining() { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
310 |
return new CollectorImpl<CharSequence, StringBuilder, String>( |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
311 |
StringBuilder::new, StringBuilder::append, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
312 |
(r1, r2) -> { r1.append(r2); return r1; }, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
313 |
StringBuilder::toString, CH_NOID); |
17196 | 314 |
} |
315 |
||
316 |
/** |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
317 |
* Returns a {@code Collector} that concatenates the input elements, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
318 |
* separated by the specified delimiter, in encounter order. |
17196 | 319 |
* |
320 |
* @param delimiter the delimiter to be used between each element |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
321 |
* @return A {@code Collector} which concatenates CharSequence elements, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
322 |
* separated by the specified delimiter, in encounter order |
17196 | 323 |
*/ |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
324 |
public static Collector<CharSequence, ?, String> joining(CharSequence delimiter) { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
325 |
return joining(delimiter, "", ""); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
326 |
} |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
327 |
|
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
328 |
/** |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
329 |
* Returns a {@code Collector} that concatenates the input elements, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
330 |
* separated by the specified delimiter, with the specified prefix and |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
331 |
* suffix, in encounter order. |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
332 |
* |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
333 |
* @param delimiter the delimiter to be used between each element |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
334 |
* @param prefix the sequence of characters to be used at the beginning |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
335 |
* of the joined result |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
336 |
* @param suffix the sequence of characters to be used at the end |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
337 |
* of the joined result |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
338 |
* @return A {@code Collector} which concatenates CharSequence elements, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
339 |
* separated by the specified delimiter, in encounter order |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
340 |
*/ |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
341 |
public static Collector<CharSequence, ?, String> joining(CharSequence delimiter, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
342 |
CharSequence prefix, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
343 |
CharSequence suffix) { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
344 |
return new CollectorImpl<>( |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
345 |
() -> new StringJoiner(delimiter, prefix, suffix), |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
346 |
StringJoiner::add, StringJoiner::merge, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
347 |
StringJoiner::toString, CH_NOID); |
17196 | 348 |
} |
349 |
||
350 |
/** |
|
351 |
* {@code BinaryOperator<Map>} that merges the contents of its right |
|
352 |
* argument into its left argument, using the provided merge function to |
|
353 |
* handle duplicate keys. |
|
354 |
* |
|
355 |
* @param <K> type of the map keys |
|
356 |
* @param <V> type of the map values |
|
357 |
* @param <M> type of the map |
|
358 |
* @param mergeFunction A merge function suitable for |
|
359 |
* {@link Map#merge(Object, Object, BiFunction) Map.merge()} |
|
360 |
* @return a merge function for two maps |
|
361 |
*/ |
|
362 |
private static <K, V, M extends Map<K,V>> |
|
363 |
BinaryOperator<M> mapMerger(BinaryOperator<V> mergeFunction) { |
|
364 |
return (m1, m2) -> { |
|
365 |
for (Map.Entry<K,V> e : m2.entrySet()) |
|
366 |
m1.merge(e.getKey(), e.getValue(), mergeFunction); |
|
367 |
return m1; |
|
368 |
}; |
|
369 |
} |
|
370 |
||
371 |
/** |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
372 |
* Adapts a {@code Collector} accepting elements of type {@code U} to one |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
373 |
* accepting elements of type {@code T} by applying a mapping function to |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
374 |
* each input element before accumulation. |
17196 | 375 |
* |
376 |
* @apiNote |
|
377 |
* The {@code mapping()} collectors are most useful when used in a |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
378 |
* multi-level reduction, such as downstream of a {@code groupingBy} or |
17196 | 379 |
* {@code partitioningBy}. For example, given a stream of |
380 |
* {@code Person}, to accumulate the set of last names in each city: |
|
381 |
* <pre>{@code |
|
382 |
* Map<City, Set<String>> lastNamesByCity |
|
383 |
* = people.stream().collect(groupingBy(Person::getCity, |
|
384 |
* mapping(Person::getLastName, toSet()))); |
|
385 |
* }</pre> |
|
386 |
* |
|
387 |
* @param <T> the type of the input elements |
|
388 |
* @param <U> type of elements accepted by downstream collector |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
389 |
* @param <A> intermediate accumulation type of the downstream collector |
17196 | 390 |
* @param <R> result type of collector |
391 |
* @param mapper a function to be applied to the input elements |
|
392 |
* @param downstream a collector which will accept mapped values |
|
393 |
* @return a collector which applies the mapping function to the input |
|
394 |
* elements and provides the mapped results to the downstream collector |
|
395 |
*/ |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
396 |
public static <T, U, A, R> |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
397 |
Collector<T, ?, R> mapping(Function<? super T, ? extends U> mapper, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
398 |
Collector<? super U, A, R> downstream) { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
399 |
BiConsumer<A, ? super U> downstreamAccumulator = downstream.accumulator(); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
400 |
return new CollectorImpl<>(downstream.supplier(), |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
401 |
(r, t) -> downstreamAccumulator.accept(r, mapper.apply(t)), |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
402 |
downstream.combiner(), downstream.finisher(), |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
403 |
downstream.characteristics()); |
17196 | 404 |
} |
405 |
||
406 |
/** |
|
29218 | 407 |
* Adapts a {@code Collector} accepting elements of type {@code U} to one |
408 |
* accepting elements of type {@code T} by applying a flat mapping function |
|
409 |
* to each input element before accumulation. The flat mapping function |
|
410 |
* maps an input element to a {@link Stream stream} covering zero or more |
|
411 |
* output elements that are then accumulated downstream. Each mapped stream |
|
412 |
* is {@link java.util.stream.BaseStream#close() closed} after its contents |
|
413 |
* have been placed downstream. (If a mapped stream is {@code null} |
|
414 |
* an empty stream is used, instead.) |
|
415 |
* |
|
416 |
* @apiNote |
|
417 |
* The {@code flatMapping()} collectors are most useful when used in a |
|
418 |
* multi-level reduction, such as downstream of a {@code groupingBy} or |
|
419 |
* {@code partitioningBy}. For example, given a stream of |
|
420 |
* {@code Order}, to accumulate the set of line items for each customer: |
|
421 |
* <pre>{@code |
|
422 |
* Map<String, Set<LineItem>> itemsByCustomerName |
|
423 |
* = orders.stream().collect(groupingBy(Order::getCustomerName, |
|
424 |
* flatMapping(order -> order.getLineItems().stream(), toSet()))); |
|
425 |
* }</pre> |
|
426 |
* |
|
427 |
* @param <T> the type of the input elements |
|
428 |
* @param <U> type of elements accepted by downstream collector |
|
429 |
* @param <A> intermediate accumulation type of the downstream collector |
|
430 |
* @param <R> result type of collector |
|
431 |
* @param mapper a function to be applied to the input elements, which |
|
432 |
* returns a stream of results |
|
433 |
* @param downstream a collector which will receive the elements of the |
|
434 |
* stream returned by mapper |
|
435 |
* @return a collector which applies the mapping function to the input |
|
436 |
* elements and provides the flat mapped results to the downstream collector |
|
437 |
* @since 1.9 |
|
438 |
*/ |
|
439 |
public static <T, U, A, R> |
|
440 |
Collector<T, ?, R> flatMapping(Function<? super T, ? extends Stream<? extends U>> mapper, |
|
441 |
Collector<? super U, A, R> downstream) { |
|
442 |
BiConsumer<A, ? super U> downstreamAccumulator = downstream.accumulator(); |
|
443 |
return new CollectorImpl<>(downstream.supplier(), |
|
444 |
(r, t) -> { |
|
445 |
try (Stream<? extends U> result = mapper.apply(t)) { |
|
446 |
if (result != null) |
|
447 |
result.sequential().forEach(u -> downstreamAccumulator.accept(r, u)); |
|
448 |
} |
|
449 |
}, |
|
450 |
downstream.combiner(), downstream.finisher(), |
|
451 |
downstream.characteristics()); |
|
452 |
} |
|
453 |
||
454 |
/** |
|
19420 | 455 |
* Adapts a {@code Collector} to perform an additional finishing |
456 |
* transformation. For example, one could adapt the {@link #toList()} |
|
457 |
* collector to always produce an immutable list with: |
|
458 |
* <pre>{@code |
|
459 |
* List<String> people |
|
460 |
* = people.stream().collect(collectingAndThen(toList(), Collections::unmodifiableList)); |
|
461 |
* }</pre> |
|
462 |
* |
|
463 |
* @param <T> the type of the input elements |
|
464 |
* @param <A> intermediate accumulation type of the downstream collector |
|
465 |
* @param <R> result type of the downstream collector |
|
466 |
* @param <RR> result type of the resulting collector |
|
467 |
* @param downstream a collector |
|
468 |
* @param finisher a function to be applied to the final result of the downstream collector |
|
469 |
* @return a collector which performs the action of the downstream collector, |
|
470 |
* followed by an additional finishing step |
|
471 |
*/ |
|
472 |
public static<T,A,R,RR> Collector<T,A,RR> collectingAndThen(Collector<T,A,R> downstream, |
|
473 |
Function<R,RR> finisher) { |
|
474 |
Set<Collector.Characteristics> characteristics = downstream.characteristics(); |
|
475 |
if (characteristics.contains(Collector.Characteristics.IDENTITY_FINISH)) { |
|
476 |
if (characteristics.size() == 1) |
|
477 |
characteristics = Collectors.CH_NOID; |
|
478 |
else { |
|
479 |
characteristics = EnumSet.copyOf(characteristics); |
|
480 |
characteristics.remove(Collector.Characteristics.IDENTITY_FINISH); |
|
481 |
characteristics = Collections.unmodifiableSet(characteristics); |
|
482 |
} |
|
483 |
} |
|
484 |
return new CollectorImpl<>(downstream.supplier(), |
|
485 |
downstream.accumulator(), |
|
486 |
downstream.combiner(), |
|
487 |
downstream.finisher().andThen(finisher), |
|
488 |
characteristics); |
|
489 |
} |
|
490 |
||
491 |
/** |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
492 |
* Returns a {@code Collector} accepting elements of type {@code T} that |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
493 |
* counts the number of input elements. If no elements are present, the |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
494 |
* result is 0. |
17196 | 495 |
* |
496 |
* @implSpec |
|
497 |
* This produces a result equivalent to: |
|
498 |
* <pre>{@code |
|
499 |
* reducing(0L, e -> 1L, Long::sum) |
|
500 |
* }</pre> |
|
501 |
* |
|
502 |
* @param <T> the type of the input elements |
|
503 |
* @return a {@code Collector} that counts the input elements |
|
504 |
*/ |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
505 |
public static <T> Collector<T, ?, Long> |
17196 | 506 |
counting() { |
507 |
return reducing(0L, e -> 1L, Long::sum); |
|
508 |
} |
|
509 |
||
510 |
/** |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
511 |
* Returns a {@code Collector} that produces the minimal element according |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
512 |
* to a given {@code Comparator}, described as an {@code Optional<T>}. |
17196 | 513 |
* |
514 |
* @implSpec |
|
515 |
* This produces a result equivalent to: |
|
516 |
* <pre>{@code |
|
18571 | 517 |
* reducing(BinaryOperator.minBy(comparator)) |
17196 | 518 |
* }</pre> |
519 |
* |
|
520 |
* @param <T> the type of the input elements |
|
521 |
* @param comparator a {@code Comparator} for comparing elements |
|
522 |
* @return a {@code Collector} that produces the minimal value |
|
523 |
*/ |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
524 |
public static <T> Collector<T, ?, Optional<T>> |
17196 | 525 |
minBy(Comparator<? super T> comparator) { |
18571 | 526 |
return reducing(BinaryOperator.minBy(comparator)); |
17196 | 527 |
} |
528 |
||
529 |
/** |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
530 |
* Returns a {@code Collector} that produces the maximal element according |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
531 |
* to a given {@code Comparator}, described as an {@code Optional<T>}. |
17196 | 532 |
* |
533 |
* @implSpec |
|
534 |
* This produces a result equivalent to: |
|
535 |
* <pre>{@code |
|
18571 | 536 |
* reducing(BinaryOperator.maxBy(comparator)) |
17196 | 537 |
* }</pre> |
538 |
* |
|
539 |
* @param <T> the type of the input elements |
|
540 |
* @param comparator a {@code Comparator} for comparing elements |
|
541 |
* @return a {@code Collector} that produces the maximal value |
|
542 |
*/ |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
543 |
public static <T> Collector<T, ?, Optional<T>> |
17196 | 544 |
maxBy(Comparator<? super T> comparator) { |
18571 | 545 |
return reducing(BinaryOperator.maxBy(comparator)); |
17196 | 546 |
} |
547 |
||
548 |
/** |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
549 |
* Returns a {@code Collector} that produces the sum of a integer-valued |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
550 |
* function applied to the input elements. If no elements are present, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
551 |
* the result is 0. |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
552 |
* |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
553 |
* @param <T> the type of the input elements |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
554 |
* @param mapper a function extracting the property to be summed |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
555 |
* @return a {@code Collector} that produces the sum of a derived property |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
556 |
*/ |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
557 |
public static <T> Collector<T, ?, Integer> |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
558 |
summingInt(ToIntFunction<? super T> mapper) { |
21339 | 559 |
return new CollectorImpl<>( |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
560 |
() -> new int[1], |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
561 |
(a, t) -> { a[0] += mapper.applyAsInt(t); }, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
562 |
(a, b) -> { a[0] += b[0]; return a; }, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
563 |
a -> a[0], CH_NOID); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
564 |
} |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
565 |
|
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
566 |
/** |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
567 |
* Returns a {@code Collector} that produces the sum of a long-valued |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
568 |
* function applied to the input elements. If no elements are present, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
569 |
* the result is 0. |
17196 | 570 |
* |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
571 |
* @param <T> the type of the input elements |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
572 |
* @param mapper a function extracting the property to be summed |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
573 |
* @return a {@code Collector} that produces the sum of a derived property |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
574 |
*/ |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
575 |
public static <T> Collector<T, ?, Long> |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
576 |
summingLong(ToLongFunction<? super T> mapper) { |
21339 | 577 |
return new CollectorImpl<>( |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
578 |
() -> new long[1], |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
579 |
(a, t) -> { a[0] += mapper.applyAsLong(t); }, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
580 |
(a, b) -> { a[0] += b[0]; return a; }, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
581 |
a -> a[0], CH_NOID); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
582 |
} |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
583 |
|
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
584 |
/** |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
585 |
* Returns a {@code Collector} that produces the sum of a double-valued |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
586 |
* function applied to the input elements. If no elements are present, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
587 |
* the result is 0. |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
588 |
* |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
589 |
* <p>The sum returned can vary depending upon the order in which |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
590 |
* values are recorded, due to accumulated rounding error in |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
591 |
* addition of values of differing magnitudes. Values sorted by increasing |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
592 |
* absolute magnitude tend to yield more accurate results. If any recorded |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
593 |
* value is a {@code NaN} or the sum is at any point a {@code NaN} then the |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
594 |
* sum will be {@code NaN}. |
17196 | 595 |
* |
596 |
* @param <T> the type of the input elements |
|
597 |
* @param mapper a function extracting the property to be summed |
|
598 |
* @return a {@code Collector} that produces the sum of a derived property |
|
599 |
*/ |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
600 |
public static <T> Collector<T, ?, Double> |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
601 |
summingDouble(ToDoubleFunction<? super T> mapper) { |
21946
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
602 |
/* |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
603 |
* In the arrays allocated for the collect operation, index 0 |
22101
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
604 |
* holds the high-order bits of the running sum, index 1 holds |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
605 |
* the low-order bits of the sum computed via compensated |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
606 |
* summation, and index 2 holds the simple sum used to compute |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
607 |
* the proper result if the stream contains infinite values of |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
608 |
* the same sign. |
21946
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
609 |
*/ |
21339 | 610 |
return new CollectorImpl<>( |
22101
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
611 |
() -> new double[3], |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
612 |
(a, t) -> { sumWithCompensation(a, mapper.applyAsDouble(t)); |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
613 |
a[2] += mapper.applyAsDouble(t);}, |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
614 |
(a, b) -> { sumWithCompensation(a, b[0]); |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
615 |
a[2] += b[2]; |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
616 |
return sumWithCompensation(a, b[1]); }, |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
617 |
a -> computeFinalSum(a), |
21946
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
618 |
CH_NOID); |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
619 |
} |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
620 |
|
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
621 |
/** |
21946
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
622 |
* Incorporate a new double value using Kahan summation / |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
623 |
* compensation summation. |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
624 |
* |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
625 |
* High-order bits of the sum are in intermediateSum[0], low-order |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
626 |
* bits of the sum are in intermediateSum[1], any additional |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
627 |
* elements are application-specific. |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
628 |
* |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
629 |
* @param intermediateSum the high-order and low-order words of the intermediate sum |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
630 |
* @param value the name value to be included in the running sum |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
631 |
*/ |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
632 |
static double[] sumWithCompensation(double[] intermediateSum, double value) { |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
633 |
double tmp = value - intermediateSum[1]; |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
634 |
double sum = intermediateSum[0]; |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
635 |
double velvel = sum + tmp; // Little wolf of rounding error |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
636 |
intermediateSum[1] = (velvel - sum) - tmp; |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
637 |
intermediateSum[0] = velvel; |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
638 |
return intermediateSum; |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
639 |
} |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
640 |
|
22101
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
641 |
/** |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
642 |
* If the compensated sum is spuriously NaN from accumulating one |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
643 |
* or more same-signed infinite values, return the |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
644 |
* correctly-signed infinity stored in the simple sum. |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
645 |
*/ |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
646 |
static double computeFinalSum(double[] summands) { |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
647 |
// Better error bounds to add both terms as the final sum |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
648 |
double tmp = summands[0] + summands[1]; |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
649 |
double simpleSum = summands[summands.length - 1]; |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
650 |
if (Double.isNaN(tmp) && Double.isInfinite(simpleSum)) |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
651 |
return simpleSum; |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
652 |
else |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
653 |
return tmp; |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
654 |
} |
21946
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
655 |
|
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
656 |
/** |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
657 |
* Returns a {@code Collector} that produces the arithmetic mean of an integer-valued |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
658 |
* function applied to the input elements. If no elements are present, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
659 |
* the result is 0. |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
660 |
* |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
661 |
* @param <T> the type of the input elements |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
662 |
* @param mapper a function extracting the property to be summed |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
663 |
* @return a {@code Collector} that produces the sum of a derived property |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
664 |
*/ |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
665 |
public static <T> Collector<T, ?, Double> |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
666 |
averagingInt(ToIntFunction<? super T> mapper) { |
21339 | 667 |
return new CollectorImpl<>( |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
668 |
() -> new long[2], |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
669 |
(a, t) -> { a[0] += mapper.applyAsInt(t); a[1]++; }, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
670 |
(a, b) -> { a[0] += b[0]; a[1] += b[1]; return a; }, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
671 |
a -> (a[1] == 0) ? 0.0d : (double) a[0] / a[1], CH_NOID); |
17196 | 672 |
} |
673 |
||
674 |
/** |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
675 |
* Returns a {@code Collector} that produces the arithmetic mean of a long-valued |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
676 |
* function applied to the input elements. If no elements are present, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
677 |
* the result is 0. |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
678 |
* |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
679 |
* @param <T> the type of the input elements |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
680 |
* @param mapper a function extracting the property to be summed |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
681 |
* @return a {@code Collector} that produces the sum of a derived property |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
682 |
*/ |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
683 |
public static <T> Collector<T, ?, Double> |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
684 |
averagingLong(ToLongFunction<? super T> mapper) { |
21339 | 685 |
return new CollectorImpl<>( |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
686 |
() -> new long[2], |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
687 |
(a, t) -> { a[0] += mapper.applyAsLong(t); a[1]++; }, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
688 |
(a, b) -> { a[0] += b[0]; a[1] += b[1]; return a; }, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
689 |
a -> (a[1] == 0) ? 0.0d : (double) a[0] / a[1], CH_NOID); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
690 |
} |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
691 |
|
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
692 |
/** |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
693 |
* Returns a {@code Collector} that produces the arithmetic mean of a double-valued |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
694 |
* function applied to the input elements. If no elements are present, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
695 |
* the result is 0. |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
696 |
* |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
697 |
* <p>The average returned can vary depending upon the order in which |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
698 |
* values are recorded, due to accumulated rounding error in |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
699 |
* addition of values of differing magnitudes. Values sorted by increasing |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
700 |
* absolute magnitude tend to yield more accurate results. If any recorded |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
701 |
* value is a {@code NaN} or the sum is at any point a {@code NaN} then the |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
702 |
* average will be {@code NaN}. |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
703 |
* |
21946
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
704 |
* @implNote The {@code double} format can represent all |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
705 |
* consecutive integers in the range -2<sup>53</sup> to |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
706 |
* 2<sup>53</sup>. If the pipeline has more than 2<sup>53</sup> |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
707 |
* values, the divisor in the average computation will saturate at |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
708 |
* 2<sup>53</sup>, leading to additional numerical errors. |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
709 |
* |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
710 |
* @param <T> the type of the input elements |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
711 |
* @param mapper a function extracting the property to be summed |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
712 |
* @return a {@code Collector} that produces the sum of a derived property |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
713 |
*/ |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
714 |
public static <T> Collector<T, ?, Double> |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
715 |
averagingDouble(ToDoubleFunction<? super T> mapper) { |
21946
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
716 |
/* |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
717 |
* In the arrays allocated for the collect operation, index 0 |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
718 |
* holds the high-order bits of the running sum, index 1 holds |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
719 |
* the low-order bits of the sum computed via compensated |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
720 |
* summation, and index 2 holds the number of values seen. |
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
721 |
*/ |
21339 | 722 |
return new CollectorImpl<>( |
22101
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
723 |
() -> new double[4], |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
724 |
(a, t) -> { sumWithCompensation(a, mapper.applyAsDouble(t)); a[2]++; a[3]+= mapper.applyAsDouble(t);}, |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
725 |
(a, b) -> { sumWithCompensation(a, b[0]); sumWithCompensation(a, b[1]); a[2] += b[2]; a[3] += b[3]; return a; }, |
231247ddf41a
8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents:
21946
diff
changeset
|
726 |
a -> (a[2] == 0) ? 0.0d : (computeFinalSum(a) / a[2]), |
21946
b4cb3bbeb52a
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents:
21339
diff
changeset
|
727 |
CH_NOID); |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
728 |
} |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
729 |
|
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
730 |
/** |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
731 |
* Returns a {@code Collector} which performs a reduction of its |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
732 |
* input elements under a specified {@code BinaryOperator} using the |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
733 |
* provided identity. |
17196 | 734 |
* |
735 |
* @apiNote |
|
736 |
* The {@code reducing()} collectors are most useful when used in a |
|
737 |
* multi-level reduction, downstream of {@code groupingBy} or |
|
738 |
* {@code partitioningBy}. To perform a simple reduction on a stream, |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
739 |
* use {@link Stream#reduce(Object, BinaryOperator)}} instead. |
17196 | 740 |
* |
741 |
* @param <T> element type for the input and output of the reduction |
|
742 |
* @param identity the identity value for the reduction (also, the value |
|
743 |
* that is returned when there are no input elements) |
|
744 |
* @param op a {@code BinaryOperator<T>} used to reduce the input elements |
|
745 |
* @return a {@code Collector} which implements the reduction operation |
|
746 |
* |
|
747 |
* @see #reducing(BinaryOperator) |
|
748 |
* @see #reducing(Object, Function, BinaryOperator) |
|
749 |
*/ |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
750 |
public static <T> Collector<T, ?, T> |
17196 | 751 |
reducing(T identity, BinaryOperator<T> op) { |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
752 |
return new CollectorImpl<>( |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
753 |
boxSupplier(identity), |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
754 |
(a, t) -> { a[0] = op.apply(a[0], t); }, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
755 |
(a, b) -> { a[0] = op.apply(a[0], b[0]); return a; }, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
756 |
a -> a[0], |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
757 |
CH_NOID); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
758 |
} |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
759 |
|
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
760 |
@SuppressWarnings("unchecked") |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
761 |
private static <T> Supplier<T[]> boxSupplier(T identity) { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
762 |
return () -> (T[]) new Object[] { identity }; |
17196 | 763 |
} |
764 |
||
765 |
/** |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
766 |
* Returns a {@code Collector} which performs a reduction of its |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
767 |
* input elements under a specified {@code BinaryOperator}. The result |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
768 |
* is described as an {@code Optional<T>}. |
17196 | 769 |
* |
770 |
* @apiNote |
|
771 |
* The {@code reducing()} collectors are most useful when used in a |
|
772 |
* multi-level reduction, downstream of {@code groupingBy} or |
|
773 |
* {@code partitioningBy}. To perform a simple reduction on a stream, |
|
774 |
* use {@link Stream#reduce(BinaryOperator)} instead. |
|
775 |
* |
|
776 |
* <p>For example, given a stream of {@code Person}, to calculate tallest |
|
777 |
* person in each city: |
|
778 |
* <pre>{@code |
|
18571 | 779 |
* Comparator<Person> byHeight = Comparator.comparing(Person::getHeight); |
25218 | 780 |
* Map<City, Optional<Person>> tallestByCity |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
781 |
* = people.stream().collect(groupingBy(Person::getCity, reducing(BinaryOperator.maxBy(byHeight)))); |
17196 | 782 |
* }</pre> |
783 |
* |
|
784 |
* @param <T> element type for the input and output of the reduction |
|
785 |
* @param op a {@code BinaryOperator<T>} used to reduce the input elements |
|
786 |
* @return a {@code Collector} which implements the reduction operation |
|
787 |
* |
|
788 |
* @see #reducing(Object, BinaryOperator) |
|
789 |
* @see #reducing(Object, Function, BinaryOperator) |
|
790 |
*/ |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
791 |
public static <T> Collector<T, ?, Optional<T>> |
17196 | 792 |
reducing(BinaryOperator<T> op) { |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
793 |
class OptionalBox implements Consumer<T> { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
794 |
T value = null; |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
795 |
boolean present = false; |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
796 |
|
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
797 |
@Override |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
798 |
public void accept(T t) { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
799 |
if (present) { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
800 |
value = op.apply(value, t); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
801 |
} |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
802 |
else { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
803 |
value = t; |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
804 |
present = true; |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
805 |
} |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
806 |
} |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
807 |
} |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
808 |
|
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
809 |
return new CollectorImpl<T, OptionalBox, Optional<T>>( |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
810 |
OptionalBox::new, OptionalBox::accept, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
811 |
(a, b) -> { if (b.present) a.accept(b.value); return a; }, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
812 |
a -> Optional.ofNullable(a.value), CH_NOID); |
17196 | 813 |
} |
814 |
||
815 |
/** |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
816 |
* Returns a {@code Collector} which performs a reduction of its |
17196 | 817 |
* input elements under a specified mapping function and |
818 |
* {@code BinaryOperator}. This is a generalization of |
|
819 |
* {@link #reducing(Object, BinaryOperator)} which allows a transformation |
|
820 |
* of the elements before reduction. |
|
821 |
* |
|
822 |
* @apiNote |
|
823 |
* The {@code reducing()} collectors are most useful when used in a |
|
824 |
* multi-level reduction, downstream of {@code groupingBy} or |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
825 |
* {@code partitioningBy}. To perform a simple map-reduce on a stream, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
826 |
* use {@link Stream#map(Function)} and {@link Stream#reduce(Object, BinaryOperator)} |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
827 |
* instead. |
17196 | 828 |
* |
829 |
* <p>For example, given a stream of {@code Person}, to calculate the longest |
|
830 |
* last name of residents in each city: |
|
831 |
* <pre>{@code |
|
18571 | 832 |
* Comparator<String> byLength = Comparator.comparing(String::length); |
17196 | 833 |
* Map<City, String> longestLastNameByCity |
834 |
* = people.stream().collect(groupingBy(Person::getCity, |
|
25218 | 835 |
* reducing("", Person::getLastName, BinaryOperator.maxBy(byLength)))); |
17196 | 836 |
* }</pre> |
837 |
* |
|
838 |
* @param <T> the type of the input elements |
|
839 |
* @param <U> the type of the mapped values |
|
840 |
* @param identity the identity value for the reduction (also, the value |
|
841 |
* that is returned when there are no input elements) |
|
842 |
* @param mapper a mapping function to apply to each input value |
|
843 |
* @param op a {@code BinaryOperator<U>} used to reduce the mapped values |
|
844 |
* @return a {@code Collector} implementing the map-reduce operation |
|
845 |
* |
|
846 |
* @see #reducing(Object, BinaryOperator) |
|
847 |
* @see #reducing(BinaryOperator) |
|
848 |
*/ |
|
849 |
public static <T, U> |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
850 |
Collector<T, ?, U> reducing(U identity, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
851 |
Function<? super T, ? extends U> mapper, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
852 |
BinaryOperator<U> op) { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
853 |
return new CollectorImpl<>( |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
854 |
boxSupplier(identity), |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
855 |
(a, t) -> { a[0] = op.apply(a[0], mapper.apply(t)); }, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
856 |
(a, b) -> { a[0] = op.apply(a[0], b[0]); return a; }, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
857 |
a -> a[0], CH_NOID); |
17196 | 858 |
} |
859 |
||
860 |
/** |
|
861 |
* Returns a {@code Collector} implementing a "group by" operation on |
|
862 |
* input elements of type {@code T}, grouping elements according to a |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
863 |
* classification function, and returning the results in a {@code Map}. |
17196 | 864 |
* |
865 |
* <p>The classification function maps elements to some key type {@code K}. |
|
866 |
* The collector produces a {@code Map<K, List<T>>} whose keys are the |
|
867 |
* values resulting from applying the classification function to the input |
|
868 |
* elements, and whose corresponding values are {@code List}s containing the |
|
869 |
* input elements which map to the associated key under the classification |
|
870 |
* function. |
|
871 |
* |
|
872 |
* <p>There are no guarantees on the type, mutability, serializability, or |
|
873 |
* thread-safety of the {@code Map} or {@code List} objects returned. |
|
874 |
* @implSpec |
|
875 |
* This produces a result similar to: |
|
876 |
* <pre>{@code |
|
877 |
* groupingBy(classifier, toList()); |
|
878 |
* }</pre> |
|
879 |
* |
|
21339 | 880 |
* @implNote |
881 |
* The returned {@code Collector} is not concurrent. For parallel stream |
|
882 |
* pipelines, the {@code combiner} function operates by merging the keys |
|
883 |
* from one map into another, which can be an expensive operation. If |
|
884 |
* preservation of the order in which elements appear in the resulting {@code Map} |
|
885 |
* collector is not required, using {@link #groupingByConcurrent(Function)} |
|
886 |
* may offer better parallel performance. |
|
887 |
* |
|
17196 | 888 |
* @param <T> the type of the input elements |
889 |
* @param <K> the type of the keys |
|
890 |
* @param classifier the classifier function mapping input elements to keys |
|
891 |
* @return a {@code Collector} implementing the group-by operation |
|
892 |
* |
|
893 |
* @see #groupingBy(Function, Collector) |
|
894 |
* @see #groupingBy(Function, Supplier, Collector) |
|
895 |
* @see #groupingByConcurrent(Function) |
|
896 |
*/ |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
897 |
public static <T, K> Collector<T, ?, Map<K, List<T>>> |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
898 |
groupingBy(Function<? super T, ? extends K> classifier) { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
899 |
return groupingBy(classifier, toList()); |
17196 | 900 |
} |
901 |
||
902 |
/** |
|
903 |
* Returns a {@code Collector} implementing a cascaded "group by" operation |
|
904 |
* on input elements of type {@code T}, grouping elements according to a |
|
905 |
* classification function, and then performing a reduction operation on |
|
906 |
* the values associated with a given key using the specified downstream |
|
907 |
* {@code Collector}. |
|
908 |
* |
|
909 |
* <p>The classification function maps elements to some key type {@code K}. |
|
910 |
* The downstream collector operates on elements of type {@code T} and |
|
911 |
* produces a result of type {@code D}. The resulting collector produces a |
|
912 |
* {@code Map<K, D>}. |
|
913 |
* |
|
914 |
* <p>There are no guarantees on the type, mutability, |
|
915 |
* serializability, or thread-safety of the {@code Map} returned. |
|
916 |
* |
|
917 |
* <p>For example, to compute the set of last names of people in each city: |
|
918 |
* <pre>{@code |
|
919 |
* Map<City, Set<String>> namesByCity |
|
920 |
* = people.stream().collect(groupingBy(Person::getCity, |
|
921 |
* mapping(Person::getLastName, toSet()))); |
|
922 |
* }</pre> |
|
923 |
* |
|
21339 | 924 |
* @implNote |
925 |
* The returned {@code Collector} is not concurrent. For parallel stream |
|
926 |
* pipelines, the {@code combiner} function operates by merging the keys |
|
927 |
* from one map into another, which can be an expensive operation. If |
|
928 |
* preservation of the order in which elements are presented to the downstream |
|
929 |
* collector is not required, using {@link #groupingByConcurrent(Function, Collector)} |
|
930 |
* may offer better parallel performance. |
|
931 |
* |
|
17196 | 932 |
* @param <T> the type of the input elements |
933 |
* @param <K> the type of the keys |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
934 |
* @param <A> the intermediate accumulation type of the downstream collector |
17196 | 935 |
* @param <D> the result type of the downstream reduction |
936 |
* @param classifier a classifier function mapping input elements to keys |
|
937 |
* @param downstream a {@code Collector} implementing the downstream reduction |
|
938 |
* @return a {@code Collector} implementing the cascaded group-by operation |
|
939 |
* @see #groupingBy(Function) |
|
940 |
* |
|
941 |
* @see #groupingBy(Function, Supplier, Collector) |
|
942 |
* @see #groupingByConcurrent(Function, Collector) |
|
943 |
*/ |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
944 |
public static <T, K, A, D> |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
945 |
Collector<T, ?, Map<K, D>> groupingBy(Function<? super T, ? extends K> classifier, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
946 |
Collector<? super T, A, D> downstream) { |
17196 | 947 |
return groupingBy(classifier, HashMap::new, downstream); |
948 |
} |
|
949 |
||
950 |
/** |
|
951 |
* Returns a {@code Collector} implementing a cascaded "group by" operation |
|
952 |
* on input elements of type {@code T}, grouping elements according to a |
|
953 |
* classification function, and then performing a reduction operation on |
|
954 |
* the values associated with a given key using the specified downstream |
|
955 |
* {@code Collector}. The {@code Map} produced by the Collector is created |
|
956 |
* with the supplied factory function. |
|
957 |
* |
|
958 |
* <p>The classification function maps elements to some key type {@code K}. |
|
959 |
* The downstream collector operates on elements of type {@code T} and |
|
960 |
* produces a result of type {@code D}. The resulting collector produces a |
|
961 |
* {@code Map<K, D>}. |
|
962 |
* |
|
963 |
* <p>For example, to compute the set of last names of people in each city, |
|
964 |
* where the city names are sorted: |
|
965 |
* <pre>{@code |
|
966 |
* Map<City, Set<String>> namesByCity |
|
967 |
* = people.stream().collect(groupingBy(Person::getCity, TreeMap::new, |
|
968 |
* mapping(Person::getLastName, toSet()))); |
|
969 |
* }</pre> |
|
970 |
* |
|
21339 | 971 |
* @implNote |
972 |
* The returned {@code Collector} is not concurrent. For parallel stream |
|
973 |
* pipelines, the {@code combiner} function operates by merging the keys |
|
974 |
* from one map into another, which can be an expensive operation. If |
|
975 |
* preservation of the order in which elements are presented to the downstream |
|
976 |
* collector is not required, using {@link #groupingByConcurrent(Function, Supplier, Collector)} |
|
977 |
* may offer better parallel performance. |
|
978 |
* |
|
17196 | 979 |
* @param <T> the type of the input elements |
980 |
* @param <K> the type of the keys |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
981 |
* @param <A> the intermediate accumulation type of the downstream collector |
17196 | 982 |
* @param <D> the result type of the downstream reduction |
983 |
* @param <M> the type of the resulting {@code Map} |
|
984 |
* @param classifier a classifier function mapping input elements to keys |
|
985 |
* @param downstream a {@code Collector} implementing the downstream reduction |
|
986 |
* @param mapFactory a function which, when called, produces a new empty |
|
987 |
* {@code Map} of the desired type |
|
988 |
* @return a {@code Collector} implementing the cascaded group-by operation |
|
989 |
* |
|
990 |
* @see #groupingBy(Function, Collector) |
|
991 |
* @see #groupingBy(Function) |
|
992 |
* @see #groupingByConcurrent(Function, Supplier, Collector) |
|
993 |
*/ |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
994 |
public static <T, K, D, A, M extends Map<K, D>> |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
995 |
Collector<T, ?, M> groupingBy(Function<? super T, ? extends K> classifier, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
996 |
Supplier<M> mapFactory, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
997 |
Collector<? super T, A, D> downstream) { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
998 |
Supplier<A> downstreamSupplier = downstream.supplier(); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
999 |
BiConsumer<A, ? super T> downstreamAccumulator = downstream.accumulator(); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1000 |
BiConsumer<Map<K, A>, T> accumulator = (m, t) -> { |
17196 | 1001 |
K key = Objects.requireNonNull(classifier.apply(t), "element cannot be mapped to a null key"); |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1002 |
A container = m.computeIfAbsent(key, k -> downstreamSupplier.get()); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1003 |
downstreamAccumulator.accept(container, t); |
17196 | 1004 |
}; |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1005 |
BinaryOperator<Map<K, A>> merger = Collectors.<K, A, Map<K, A>>mapMerger(downstream.combiner()); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1006 |
@SuppressWarnings("unchecked") |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1007 |
Supplier<Map<K, A>> mangledFactory = (Supplier<Map<K, A>>) mapFactory; |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1008 |
|
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1009 |
if (downstream.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1010 |
return new CollectorImpl<>(mangledFactory, accumulator, merger, CH_ID); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1011 |
} |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1012 |
else { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1013 |
@SuppressWarnings("unchecked") |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1014 |
Function<A, A> downstreamFinisher = (Function<A, A>) downstream.finisher(); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1015 |
Function<Map<K, A>, M> finisher = intermediate -> { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1016 |
intermediate.replaceAll((k, v) -> downstreamFinisher.apply(v)); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1017 |
@SuppressWarnings("unchecked") |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1018 |
M castResult = (M) intermediate; |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1019 |
return castResult; |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1020 |
}; |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1021 |
return new CollectorImpl<>(mangledFactory, accumulator, merger, finisher, CH_NOID); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1022 |
} |
17196 | 1023 |
} |
1024 |
||
1025 |
/** |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1026 |
* Returns a concurrent {@code Collector} implementing a "group by" |
17196 | 1027 |
* operation on input elements of type {@code T}, grouping elements |
1028 |
* according to a classification function. |
|
1029 |
* |
|
1030 |
* <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and |
|
1031 |
* {@link Collector.Characteristics#UNORDERED unordered} Collector. |
|
1032 |
* |
|
1033 |
* <p>The classification function maps elements to some key type {@code K}. |
|
1034 |
* The collector produces a {@code ConcurrentMap<K, List<T>>} whose keys are the |
|
1035 |
* values resulting from applying the classification function to the input |
|
1036 |
* elements, and whose corresponding values are {@code List}s containing the |
|
1037 |
* input elements which map to the associated key under the classification |
|
1038 |
* function. |
|
1039 |
* |
|
1040 |
* <p>There are no guarantees on the type, mutability, or serializability |
|
28419
361e3aa27cb5
8068599: Add mutability, serializability, and thread-safety, caveat to all Collectors that do not accept a Collection supplier
chegar
parents:
25859
diff
changeset
|
1041 |
* of the {@code ConcurrentMap} or {@code List} objects returned, or of the |
17196 | 1042 |
* thread-safety of the {@code List} objects returned. |
1043 |
* @implSpec |
|
1044 |
* This produces a result similar to: |
|
1045 |
* <pre>{@code |
|
1046 |
* groupingByConcurrent(classifier, toList()); |
|
1047 |
* }</pre> |
|
1048 |
* |
|
1049 |
* @param <T> the type of the input elements |
|
1050 |
* @param <K> the type of the keys |
|
1051 |
* @param classifier a classifier function mapping input elements to keys |
|
21339 | 1052 |
* @return a concurrent, unordered {@code Collector} implementing the group-by operation |
17196 | 1053 |
* |
1054 |
* @see #groupingBy(Function) |
|
1055 |
* @see #groupingByConcurrent(Function, Collector) |
|
1056 |
* @see #groupingByConcurrent(Function, Supplier, Collector) |
|
1057 |
*/ |
|
1058 |
public static <T, K> |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1059 |
Collector<T, ?, ConcurrentMap<K, List<T>>> |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1060 |
groupingByConcurrent(Function<? super T, ? extends K> classifier) { |
17196 | 1061 |
return groupingByConcurrent(classifier, ConcurrentHashMap::new, toList()); |
1062 |
} |
|
1063 |
||
1064 |
/** |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1065 |
* Returns a concurrent {@code Collector} implementing a cascaded "group by" |
17196 | 1066 |
* operation on input elements of type {@code T}, grouping elements |
1067 |
* according to a classification function, and then performing a reduction |
|
1068 |
* operation on the values associated with a given key using the specified |
|
1069 |
* downstream {@code Collector}. |
|
1070 |
* |
|
1071 |
* <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and |
|
1072 |
* {@link Collector.Characteristics#UNORDERED unordered} Collector. |
|
1073 |
* |
|
1074 |
* <p>The classification function maps elements to some key type {@code K}. |
|
1075 |
* The downstream collector operates on elements of type {@code T} and |
|
1076 |
* produces a result of type {@code D}. The resulting collector produces a |
|
1077 |
* {@code Map<K, D>}. |
|
1078 |
* |
|
28419
361e3aa27cb5
8068599: Add mutability, serializability, and thread-safety, caveat to all Collectors that do not accept a Collection supplier
chegar
parents:
25859
diff
changeset
|
1079 |
* <p>There are no guarantees on the type, mutability, or serializability |
361e3aa27cb5
8068599: Add mutability, serializability, and thread-safety, caveat to all Collectors that do not accept a Collection supplier
chegar
parents:
25859
diff
changeset
|
1080 |
* of the {@code ConcurrentMap} returned. |
361e3aa27cb5
8068599: Add mutability, serializability, and thread-safety, caveat to all Collectors that do not accept a Collection supplier
chegar
parents:
25859
diff
changeset
|
1081 |
* |
17196 | 1082 |
* <p>For example, to compute the set of last names of people in each city, |
1083 |
* where the city names are sorted: |
|
1084 |
* <pre>{@code |
|
1085 |
* ConcurrentMap<City, Set<String>> namesByCity |
|
19850 | 1086 |
* = people.stream().collect(groupingByConcurrent(Person::getCity, |
17196 | 1087 |
* mapping(Person::getLastName, toSet()))); |
1088 |
* }</pre> |
|
1089 |
* |
|
1090 |
* @param <T> the type of the input elements |
|
1091 |
* @param <K> the type of the keys |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1092 |
* @param <A> the intermediate accumulation type of the downstream collector |
17196 | 1093 |
* @param <D> the result type of the downstream reduction |
1094 |
* @param classifier a classifier function mapping input elements to keys |
|
1095 |
* @param downstream a {@code Collector} implementing the downstream reduction |
|
21339 | 1096 |
* @return a concurrent, unordered {@code Collector} implementing the cascaded group-by operation |
17196 | 1097 |
* |
1098 |
* @see #groupingBy(Function, Collector) |
|
1099 |
* @see #groupingByConcurrent(Function) |
|
1100 |
* @see #groupingByConcurrent(Function, Supplier, Collector) |
|
1101 |
*/ |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1102 |
public static <T, K, A, D> |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1103 |
Collector<T, ?, ConcurrentMap<K, D>> groupingByConcurrent(Function<? super T, ? extends K> classifier, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1104 |
Collector<? super T, A, D> downstream) { |
17196 | 1105 |
return groupingByConcurrent(classifier, ConcurrentHashMap::new, downstream); |
1106 |
} |
|
1107 |
||
1108 |
/** |
|
1109 |
* Returns a concurrent {@code Collector} implementing a cascaded "group by" |
|
1110 |
* operation on input elements of type {@code T}, grouping elements |
|
1111 |
* according to a classification function, and then performing a reduction |
|
1112 |
* operation on the values associated with a given key using the specified |
|
1113 |
* downstream {@code Collector}. The {@code ConcurrentMap} produced by the |
|
1114 |
* Collector is created with the supplied factory function. |
|
1115 |
* |
|
1116 |
* <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and |
|
1117 |
* {@link Collector.Characteristics#UNORDERED unordered} Collector. |
|
1118 |
* |
|
1119 |
* <p>The classification function maps elements to some key type {@code K}. |
|
1120 |
* The downstream collector operates on elements of type {@code T} and |
|
1121 |
* produces a result of type {@code D}. The resulting collector produces a |
|
1122 |
* {@code Map<K, D>}. |
|
1123 |
* |
|
1124 |
* <p>For example, to compute the set of last names of people in each city, |
|
1125 |
* where the city names are sorted: |
|
1126 |
* <pre>{@code |
|
1127 |
* ConcurrentMap<City, Set<String>> namesByCity |
|
1128 |
* = people.stream().collect(groupingBy(Person::getCity, ConcurrentSkipListMap::new, |
|
1129 |
* mapping(Person::getLastName, toSet()))); |
|
1130 |
* }</pre> |
|
1131 |
* |
|
1132 |
* |
|
1133 |
* @param <T> the type of the input elements |
|
1134 |
* @param <K> the type of the keys |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1135 |
* @param <A> the intermediate accumulation type of the downstream collector |
17196 | 1136 |
* @param <D> the result type of the downstream reduction |
1137 |
* @param <M> the type of the resulting {@code ConcurrentMap} |
|
1138 |
* @param classifier a classifier function mapping input elements to keys |
|
1139 |
* @param downstream a {@code Collector} implementing the downstream reduction |
|
1140 |
* @param mapFactory a function which, when called, produces a new empty |
|
1141 |
* {@code ConcurrentMap} of the desired type |
|
21339 | 1142 |
* @return a concurrent, unordered {@code Collector} implementing the cascaded group-by operation |
17196 | 1143 |
* |
1144 |
* @see #groupingByConcurrent(Function) |
|
1145 |
* @see #groupingByConcurrent(Function, Collector) |
|
1146 |
* @see #groupingBy(Function, Supplier, Collector) |
|
1147 |
*/ |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1148 |
public static <T, K, A, D, M extends ConcurrentMap<K, D>> |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1149 |
Collector<T, ?, M> groupingByConcurrent(Function<? super T, ? extends K> classifier, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1150 |
Supplier<M> mapFactory, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1151 |
Collector<? super T, A, D> downstream) { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1152 |
Supplier<A> downstreamSupplier = downstream.supplier(); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1153 |
BiConsumer<A, ? super T> downstreamAccumulator = downstream.accumulator(); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1154 |
BinaryOperator<ConcurrentMap<K, A>> merger = Collectors.<K, A, ConcurrentMap<K, A>>mapMerger(downstream.combiner()); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1155 |
@SuppressWarnings("unchecked") |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1156 |
Supplier<ConcurrentMap<K, A>> mangledFactory = (Supplier<ConcurrentMap<K, A>>) mapFactory; |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1157 |
BiConsumer<ConcurrentMap<K, A>, T> accumulator; |
17196 | 1158 |
if (downstream.characteristics().contains(Collector.Characteristics.CONCURRENT)) { |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1159 |
accumulator = (m, t) -> { |
17196 | 1160 |
K key = Objects.requireNonNull(classifier.apply(t), "element cannot be mapped to a null key"); |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1161 |
A resultContainer = m.computeIfAbsent(key, k -> downstreamSupplier.get()); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1162 |
downstreamAccumulator.accept(resultContainer, t); |
17196 | 1163 |
}; |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1164 |
} |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1165 |
else { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1166 |
accumulator = (m, t) -> { |
17196 | 1167 |
K key = Objects.requireNonNull(classifier.apply(t), "element cannot be mapped to a null key"); |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1168 |
A resultContainer = m.computeIfAbsent(key, k -> downstreamSupplier.get()); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1169 |
synchronized (resultContainer) { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1170 |
downstreamAccumulator.accept(resultContainer, t); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1171 |
} |
17196 | 1172 |
}; |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1173 |
} |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1174 |
|
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1175 |
if (downstream.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1176 |
return new CollectorImpl<>(mangledFactory, accumulator, merger, CH_CONCURRENT_ID); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1177 |
} |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1178 |
else { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1179 |
@SuppressWarnings("unchecked") |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1180 |
Function<A, A> downstreamFinisher = (Function<A, A>) downstream.finisher(); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1181 |
Function<ConcurrentMap<K, A>, M> finisher = intermediate -> { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1182 |
intermediate.replaceAll((k, v) -> downstreamFinisher.apply(v)); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1183 |
@SuppressWarnings("unchecked") |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1184 |
M castResult = (M) intermediate; |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1185 |
return castResult; |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1186 |
}; |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1187 |
return new CollectorImpl<>(mangledFactory, accumulator, merger, finisher, CH_CONCURRENT_NOID); |
17196 | 1188 |
} |
1189 |
} |
|
1190 |
||
1191 |
/** |
|
1192 |
* Returns a {@code Collector} which partitions the input elements according |
|
1193 |
* to a {@code Predicate}, and organizes them into a |
|
1194 |
* {@code Map<Boolean, List<T>>}. |
|
1195 |
* |
|
1196 |
* There are no guarantees on the type, mutability, |
|
28419
361e3aa27cb5
8068599: Add mutability, serializability, and thread-safety, caveat to all Collectors that do not accept a Collection supplier
chegar
parents:
25859
diff
changeset
|
1197 |
* serializability, or thread-safety of the {@code Map} or {@code List} |
361e3aa27cb5
8068599: Add mutability, serializability, and thread-safety, caveat to all Collectors that do not accept a Collection supplier
chegar
parents:
25859
diff
changeset
|
1198 |
* returned. |
17196 | 1199 |
* |
1200 |
* @param <T> the type of the input elements |
|
1201 |
* @param predicate a predicate used for classifying input elements |
|
1202 |
* @return a {@code Collector} implementing the partitioning operation |
|
1203 |
* |
|
1204 |
* @see #partitioningBy(Predicate, Collector) |
|
1205 |
*/ |
|
1206 |
public static <T> |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1207 |
Collector<T, ?, Map<Boolean, List<T>>> partitioningBy(Predicate<? super T> predicate) { |
17196 | 1208 |
return partitioningBy(predicate, toList()); |
1209 |
} |
|
1210 |
||
1211 |
/** |
|
1212 |
* Returns a {@code Collector} which partitions the input elements according |
|
1213 |
* to a {@code Predicate}, reduces the values in each partition according to |
|
1214 |
* another {@code Collector}, and organizes them into a |
|
1215 |
* {@code Map<Boolean, D>} whose values are the result of the downstream |
|
1216 |
* reduction. |
|
1217 |
* |
|
1218 |
* <p>There are no guarantees on the type, mutability, |
|
1219 |
* serializability, or thread-safety of the {@code Map} returned. |
|
1220 |
* |
|
1221 |
* @param <T> the type of the input elements |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1222 |
* @param <A> the intermediate accumulation type of the downstream collector |
17196 | 1223 |
* @param <D> the result type of the downstream reduction |
1224 |
* @param predicate a predicate used for classifying input elements |
|
1225 |
* @param downstream a {@code Collector} implementing the downstream |
|
1226 |
* reduction |
|
1227 |
* @return a {@code Collector} implementing the cascaded partitioning |
|
1228 |
* operation |
|
1229 |
* |
|
1230 |
* @see #partitioningBy(Predicate) |
|
1231 |
*/ |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1232 |
public static <T, D, A> |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1233 |
Collector<T, ?, Map<Boolean, D>> partitioningBy(Predicate<? super T> predicate, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1234 |
Collector<? super T, A, D> downstream) { |
19593 | 1235 |
BiConsumer<A, ? super T> downstreamAccumulator = downstream.accumulator(); |
1236 |
BiConsumer<Partition<A>, T> accumulator = (result, t) -> |
|
1237 |
downstreamAccumulator.accept(predicate.test(t) ? result.forTrue : result.forFalse, t); |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1238 |
BinaryOperator<A> op = downstream.combiner(); |
19593 | 1239 |
BinaryOperator<Partition<A>> merger = (left, right) -> |
1240 |
new Partition<>(op.apply(left.forTrue, right.forTrue), |
|
1241 |
op.apply(left.forFalse, right.forFalse)); |
|
1242 |
Supplier<Partition<A>> supplier = () -> |
|
1243 |
new Partition<>(downstream.supplier().get(), |
|
1244 |
downstream.supplier().get()); |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1245 |
if (downstream.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1246 |
return new CollectorImpl<>(supplier, accumulator, merger, CH_ID); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1247 |
} |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1248 |
else { |
19593 | 1249 |
Function<Partition<A>, Map<Boolean, D>> finisher = par -> |
1250 |
new Partition<>(downstream.finisher().apply(par.forTrue), |
|
1251 |
downstream.finisher().apply(par.forFalse)); |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1252 |
return new CollectorImpl<>(supplier, accumulator, merger, finisher, CH_NOID); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1253 |
} |
17196 | 1254 |
} |
1255 |
||
1256 |
/** |
|
21339 | 1257 |
* Returns a {@code Collector} that accumulates elements into a |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1258 |
* {@code Map} whose keys and values are the result of applying the provided |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1259 |
* mapping functions to the input elements. |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1260 |
* |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1261 |
* <p>If the mapped keys contains duplicates (according to |
17196 | 1262 |
* {@link Object#equals(Object)}), an {@code IllegalStateException} is |
1263 |
* thrown when the collection operation is performed. If the mapped keys |
|
1264 |
* may have duplicates, use {@link #toMap(Function, Function, BinaryOperator)} |
|
1265 |
* instead. |
|
1266 |
* |
|
28419
361e3aa27cb5
8068599: Add mutability, serializability, and thread-safety, caveat to all Collectors that do not accept a Collection supplier
chegar
parents:
25859
diff
changeset
|
1267 |
* <p>There are no guarantees on the type, mutability, serializability, |
361e3aa27cb5
8068599: Add mutability, serializability, and thread-safety, caveat to all Collectors that do not accept a Collection supplier
chegar
parents:
25859
diff
changeset
|
1268 |
* or thread-safety of the {@code Map} returned. |
361e3aa27cb5
8068599: Add mutability, serializability, and thread-safety, caveat to all Collectors that do not accept a Collection supplier
chegar
parents:
25859
diff
changeset
|
1269 |
* |
17196 | 1270 |
* @apiNote |
1271 |
* It is common for either the key or the value to be the input elements. |
|
1272 |
* In this case, the utility method |
|
1273 |
* {@link java.util.function.Function#identity()} may be helpful. |
|
1274 |
* For example, the following produces a {@code Map} mapping |
|
1275 |
* students to their grade point average: |
|
1276 |
* <pre>{@code |
|
1277 |
* Map<Student, Double> studentToGPA |
|
25224
7c7e029826e1
8043327: Collectors.toMap studentToGPA example uses Functions.identity()
psandoz
parents:
25218
diff
changeset
|
1278 |
* students.stream().collect(toMap(Function.identity(), |
17196 | 1279 |
* student -> computeGPA(student))); |
1280 |
* }</pre> |
|
1281 |
* And the following produces a {@code Map} mapping a unique identifier to |
|
1282 |
* students: |
|
1283 |
* <pre>{@code |
|
1284 |
* Map<String, Student> studentIdToStudent |
|
1285 |
* students.stream().collect(toMap(Student::getId, |
|
25224
7c7e029826e1
8043327: Collectors.toMap studentToGPA example uses Functions.identity()
psandoz
parents:
25218
diff
changeset
|
1286 |
* Function.identity()); |
17196 | 1287 |
* }</pre> |
1288 |
* |
|
21339 | 1289 |
* @implNote |
1290 |
* The returned {@code Collector} is not concurrent. For parallel stream |
|
1291 |
* pipelines, the {@code combiner} function operates by merging the keys |
|
1292 |
* from one map into another, which can be an expensive operation. If it is |
|
1293 |
* not required that results are inserted into the {@code Map} in encounter |
|
1294 |
* order, using {@link #toConcurrentMap(Function, Function)} |
|
1295 |
* may offer better parallel performance. |
|
1296 |
* |
|
17196 | 1297 |
* @param <T> the type of the input elements |
1298 |
* @param <K> the output type of the key mapping function |
|
1299 |
* @param <U> the output type of the value mapping function |
|
1300 |
* @param keyMapper a mapping function to produce keys |
|
1301 |
* @param valueMapper a mapping function to produce values |
|
1302 |
* @return a {@code Collector} which collects elements into a {@code Map} |
|
1303 |
* whose keys and values are the result of applying mapping functions to |
|
1304 |
* the input elements |
|
1305 |
* |
|
1306 |
* @see #toMap(Function, Function, BinaryOperator) |
|
1307 |
* @see #toMap(Function, Function, BinaryOperator, Supplier) |
|
1308 |
* @see #toConcurrentMap(Function, Function) |
|
1309 |
*/ |
|
1310 |
public static <T, K, U> |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1311 |
Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1312 |
Function<? super T, ? extends U> valueMapper) { |
24123
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
1313 |
return new CollectorImpl<>(HashMap::new, |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
1314 |
uniqKeysMapAccumulator(keyMapper, valueMapper), |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
1315 |
uniqKeysMapMerger(), |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
1316 |
CH_ID); |
17196 | 1317 |
} |
1318 |
||
1319 |
/** |
|
21339 | 1320 |
* Returns a {@code Collector} that accumulates elements into a |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1321 |
* {@code Map} whose keys and values are the result of applying the provided |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1322 |
* mapping functions to the input elements. |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1323 |
* |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1324 |
* <p>If the mapped |
17196 | 1325 |
* keys contains duplicates (according to {@link Object#equals(Object)}), |
1326 |
* the value mapping function is applied to each equal element, and the |
|
1327 |
* results are merged using the provided merging function. |
|
1328 |
* |
|
28419
361e3aa27cb5
8068599: Add mutability, serializability, and thread-safety, caveat to all Collectors that do not accept a Collection supplier
chegar
parents:
25859
diff
changeset
|
1329 |
* <p>There are no guarantees on the type, mutability, serializability, |
361e3aa27cb5
8068599: Add mutability, serializability, and thread-safety, caveat to all Collectors that do not accept a Collection supplier
chegar
parents:
25859
diff
changeset
|
1330 |
* or thread-safety of the {@code Map} returned. |
361e3aa27cb5
8068599: Add mutability, serializability, and thread-safety, caveat to all Collectors that do not accept a Collection supplier
chegar
parents:
25859
diff
changeset
|
1331 |
* |
17196 | 1332 |
* @apiNote |
1333 |
* There are multiple ways to deal with collisions between multiple elements |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1334 |
* mapping to the same key. The other forms of {@code toMap} simply use |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1335 |
* a merge function that throws unconditionally, but you can easily write |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1336 |
* more flexible merge policies. For example, if you have a stream |
17196 | 1337 |
* of {@code Person}, and you want to produce a "phone book" mapping name to |
1338 |
* address, but it is possible that two persons have the same name, you can |
|
1339 |
* do as follows to gracefully deals with these collisions, and produce a |
|
1340 |
* {@code Map} mapping names to a concatenated list of addresses: |
|
1341 |
* <pre>{@code |
|
1342 |
* Map<String, String> phoneBook |
|
1343 |
* people.stream().collect(toMap(Person::getName, |
|
1344 |
* Person::getAddress, |
|
1345 |
* (s, a) -> s + ", " + a)); |
|
1346 |
* }</pre> |
|
1347 |
* |
|
21339 | 1348 |
* @implNote |
1349 |
* The returned {@code Collector} is not concurrent. For parallel stream |
|
1350 |
* pipelines, the {@code combiner} function operates by merging the keys |
|
1351 |
* from one map into another, which can be an expensive operation. If it is |
|
1352 |
* not required that results are merged into the {@code Map} in encounter |
|
1353 |
* order, using {@link #toConcurrentMap(Function, Function, BinaryOperator)} |
|
1354 |
* may offer better parallel performance. |
|
1355 |
* |
|
17196 | 1356 |
* @param <T> the type of the input elements |
1357 |
* @param <K> the output type of the key mapping function |
|
1358 |
* @param <U> the output type of the value mapping function |
|
1359 |
* @param keyMapper a mapping function to produce keys |
|
1360 |
* @param valueMapper a mapping function to produce values |
|
1361 |
* @param mergeFunction a merge function, used to resolve collisions between |
|
1362 |
* values associated with the same key, as supplied |
|
1363 |
* to {@link Map#merge(Object, Object, BiFunction)} |
|
1364 |
* @return a {@code Collector} which collects elements into a {@code Map} |
|
1365 |
* whose keys are the result of applying a key mapping function to the input |
|
1366 |
* elements, and whose values are the result of applying a value mapping |
|
1367 |
* function to all input elements equal to the key and combining them |
|
1368 |
* using the merge function |
|
1369 |
* |
|
1370 |
* @see #toMap(Function, Function) |
|
1371 |
* @see #toMap(Function, Function, BinaryOperator, Supplier) |
|
1372 |
* @see #toConcurrentMap(Function, Function, BinaryOperator) |
|
1373 |
*/ |
|
1374 |
public static <T, K, U> |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1375 |
Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1376 |
Function<? super T, ? extends U> valueMapper, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1377 |
BinaryOperator<U> mergeFunction) { |
17196 | 1378 |
return toMap(keyMapper, valueMapper, mergeFunction, HashMap::new); |
1379 |
} |
|
1380 |
||
1381 |
/** |
|
21339 | 1382 |
* Returns a {@code Collector} that accumulates elements into a |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1383 |
* {@code Map} whose keys and values are the result of applying the provided |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1384 |
* mapping functions to the input elements. |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1385 |
* |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1386 |
* <p>If the mapped |
17196 | 1387 |
* keys contains duplicates (according to {@link Object#equals(Object)}), |
1388 |
* the value mapping function is applied to each equal element, and the |
|
1389 |
* results are merged using the provided merging function. The {@code Map} |
|
1390 |
* is created by a provided supplier function. |
|
1391 |
* |
|
21339 | 1392 |
* @implNote |
1393 |
* The returned {@code Collector} is not concurrent. For parallel stream |
|
1394 |
* pipelines, the {@code combiner} function operates by merging the keys |
|
1395 |
* from one map into another, which can be an expensive operation. If it is |
|
1396 |
* not required that results are merged into the {@code Map} in encounter |
|
1397 |
* order, using {@link #toConcurrentMap(Function, Function, BinaryOperator, Supplier)} |
|
1398 |
* may offer better parallel performance. |
|
1399 |
* |
|
17196 | 1400 |
* @param <T> the type of the input elements |
1401 |
* @param <K> the output type of the key mapping function |
|
1402 |
* @param <U> the output type of the value mapping function |
|
1403 |
* @param <M> the type of the resulting {@code Map} |
|
1404 |
* @param keyMapper a mapping function to produce keys |
|
1405 |
* @param valueMapper a mapping function to produce values |
|
1406 |
* @param mergeFunction a merge function, used to resolve collisions between |
|
1407 |
* values associated with the same key, as supplied |
|
1408 |
* to {@link Map#merge(Object, Object, BiFunction)} |
|
1409 |
* @param mapSupplier a function which returns a new, empty {@code Map} into |
|
1410 |
* which the results will be inserted |
|
1411 |
* @return a {@code Collector} which collects elements into a {@code Map} |
|
1412 |
* whose keys are the result of applying a key mapping function to the input |
|
1413 |
* elements, and whose values are the result of applying a value mapping |
|
1414 |
* function to all input elements equal to the key and combining them |
|
1415 |
* using the merge function |
|
1416 |
* |
|
1417 |
* @see #toMap(Function, Function) |
|
1418 |
* @see #toMap(Function, Function, BinaryOperator) |
|
1419 |
* @see #toConcurrentMap(Function, Function, BinaryOperator, Supplier) |
|
1420 |
*/ |
|
1421 |
public static <T, K, U, M extends Map<K, U>> |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1422 |
Collector<T, ?, M> toMap(Function<? super T, ? extends K> keyMapper, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1423 |
Function<? super T, ? extends U> valueMapper, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1424 |
BinaryOperator<U> mergeFunction, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1425 |
Supplier<M> mapSupplier) { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1426 |
BiConsumer<M, T> accumulator |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1427 |
= (map, element) -> map.merge(keyMapper.apply(element), |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1428 |
valueMapper.apply(element), mergeFunction); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1429 |
return new CollectorImpl<>(mapSupplier, accumulator, mapMerger(mergeFunction), CH_ID); |
17196 | 1430 |
} |
1431 |
||
1432 |
/** |
|
21339 | 1433 |
* Returns a concurrent {@code Collector} that accumulates elements into a |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1434 |
* {@code ConcurrentMap} whose keys and values are the result of applying |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1435 |
* the provided mapping functions to the input elements. |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1436 |
* |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1437 |
* <p>If the mapped keys contains duplicates (according to |
17196 | 1438 |
* {@link Object#equals(Object)}), an {@code IllegalStateException} is |
1439 |
* thrown when the collection operation is performed. If the mapped keys |
|
1440 |
* may have duplicates, use |
|
1441 |
* {@link #toConcurrentMap(Function, Function, BinaryOperator)} instead. |
|
1442 |
* |
|
28419
361e3aa27cb5
8068599: Add mutability, serializability, and thread-safety, caveat to all Collectors that do not accept a Collection supplier
chegar
parents:
25859
diff
changeset
|
1443 |
* <p>There are no guarantees on the type, mutability, or serializability |
361e3aa27cb5
8068599: Add mutability, serializability, and thread-safety, caveat to all Collectors that do not accept a Collection supplier
chegar
parents:
25859
diff
changeset
|
1444 |
* of the {@code ConcurrentMap} returned. |
361e3aa27cb5
8068599: Add mutability, serializability, and thread-safety, caveat to all Collectors that do not accept a Collection supplier
chegar
parents:
25859
diff
changeset
|
1445 |
* |
17196 | 1446 |
* @apiNote |
1447 |
* It is common for either the key or the value to be the input elements. |
|
1448 |
* In this case, the utility method |
|
1449 |
* {@link java.util.function.Function#identity()} may be helpful. |
|
1450 |
* For example, the following produces a {@code Map} mapping |
|
1451 |
* students to their grade point average: |
|
1452 |
* <pre>{@code |
|
1453 |
* Map<Student, Double> studentToGPA |
|
25224
7c7e029826e1
8043327: Collectors.toMap studentToGPA example uses Functions.identity()
psandoz
parents:
25218
diff
changeset
|
1454 |
* students.stream().collect(toMap(Function.identity(), |
17196 | 1455 |
* student -> computeGPA(student))); |
1456 |
* }</pre> |
|
1457 |
* And the following produces a {@code Map} mapping a unique identifier to |
|
1458 |
* students: |
|
1459 |
* <pre>{@code |
|
1460 |
* Map<String, Student> studentIdToStudent |
|
1461 |
* students.stream().collect(toConcurrentMap(Student::getId, |
|
25224
7c7e029826e1
8043327: Collectors.toMap studentToGPA example uses Functions.identity()
psandoz
parents:
25218
diff
changeset
|
1462 |
* Function.identity()); |
17196 | 1463 |
* }</pre> |
1464 |
* |
|
1465 |
* <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and |
|
1466 |
* {@link Collector.Characteristics#UNORDERED unordered} Collector. |
|
1467 |
* |
|
1468 |
* @param <T> the type of the input elements |
|
1469 |
* @param <K> the output type of the key mapping function |
|
1470 |
* @param <U> the output type of the value mapping function |
|
1471 |
* @param keyMapper the mapping function to produce keys |
|
1472 |
* @param valueMapper the mapping function to produce values |
|
21339 | 1473 |
* @return a concurrent, unordered {@code Collector} which collects elements into a |
17196 | 1474 |
* {@code ConcurrentMap} whose keys are the result of applying a key mapping |
1475 |
* function to the input elements, and whose values are the result of |
|
1476 |
* applying a value mapping function to the input elements |
|
1477 |
* |
|
1478 |
* @see #toMap(Function, Function) |
|
1479 |
* @see #toConcurrentMap(Function, Function, BinaryOperator) |
|
1480 |
* @see #toConcurrentMap(Function, Function, BinaryOperator, Supplier) |
|
1481 |
*/ |
|
1482 |
public static <T, K, U> |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1483 |
Collector<T, ?, ConcurrentMap<K,U>> toConcurrentMap(Function<? super T, ? extends K> keyMapper, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1484 |
Function<? super T, ? extends U> valueMapper) { |
24123
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
1485 |
return new CollectorImpl<>(ConcurrentHashMap::new, |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
1486 |
uniqKeysMapAccumulator(keyMapper, valueMapper), |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
1487 |
uniqKeysMapMerger(), |
7c35c9e15f8e
8040892: Incorrect message in Exception thrown by Collectors.toMap(Function,Function)
plevart
parents:
22101
diff
changeset
|
1488 |
CH_CONCURRENT_ID); |
17196 | 1489 |
} |
1490 |
||
1491 |
/** |
|
21339 | 1492 |
* Returns a concurrent {@code Collector} that accumulates elements into a |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1493 |
* {@code ConcurrentMap} whose keys and values are the result of applying |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1494 |
* the provided mapping functions to the input elements. |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1495 |
* |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1496 |
* <p>If the mapped keys contains duplicates (according to {@link Object#equals(Object)}), |
17196 | 1497 |
* the value mapping function is applied to each equal element, and the |
1498 |
* results are merged using the provided merging function. |
|
1499 |
* |
|
28419
361e3aa27cb5
8068599: Add mutability, serializability, and thread-safety, caveat to all Collectors that do not accept a Collection supplier
chegar
parents:
25859
diff
changeset
|
1500 |
* <p>There are no guarantees on the type, mutability, or serializability |
361e3aa27cb5
8068599: Add mutability, serializability, and thread-safety, caveat to all Collectors that do not accept a Collection supplier
chegar
parents:
25859
diff
changeset
|
1501 |
* of the {@code ConcurrentMap} returned. |
361e3aa27cb5
8068599: Add mutability, serializability, and thread-safety, caveat to all Collectors that do not accept a Collection supplier
chegar
parents:
25859
diff
changeset
|
1502 |
* |
17196 | 1503 |
* @apiNote |
1504 |
* There are multiple ways to deal with collisions between multiple elements |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1505 |
* mapping to the same key. The other forms of {@code toConcurrentMap} simply use |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1506 |
* a merge function that throws unconditionally, but you can easily write |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1507 |
* more flexible merge policies. For example, if you have a stream |
17196 | 1508 |
* of {@code Person}, and you want to produce a "phone book" mapping name to |
1509 |
* address, but it is possible that two persons have the same name, you can |
|
1510 |
* do as follows to gracefully deals with these collisions, and produce a |
|
1511 |
* {@code Map} mapping names to a concatenated list of addresses: |
|
1512 |
* <pre>{@code |
|
1513 |
* Map<String, String> phoneBook |
|
1514 |
* people.stream().collect(toConcurrentMap(Person::getName, |
|
1515 |
* Person::getAddress, |
|
1516 |
* (s, a) -> s + ", " + a)); |
|
1517 |
* }</pre> |
|
1518 |
* |
|
1519 |
* <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and |
|
1520 |
* {@link Collector.Characteristics#UNORDERED unordered} Collector. |
|
1521 |
* |
|
1522 |
* @param <T> the type of the input elements |
|
1523 |
* @param <K> the output type of the key mapping function |
|
1524 |
* @param <U> the output type of the value mapping function |
|
1525 |
* @param keyMapper a mapping function to produce keys |
|
1526 |
* @param valueMapper a mapping function to produce values |
|
1527 |
* @param mergeFunction a merge function, used to resolve collisions between |
|
1528 |
* values associated with the same key, as supplied |
|
1529 |
* to {@link Map#merge(Object, Object, BiFunction)} |
|
21339 | 1530 |
* @return a concurrent, unordered {@code Collector} which collects elements into a |
17196 | 1531 |
* {@code ConcurrentMap} whose keys are the result of applying a key mapping |
1532 |
* function to the input elements, and whose values are the result of |
|
1533 |
* applying a value mapping function to all input elements equal to the key |
|
1534 |
* and combining them using the merge function |
|
1535 |
* |
|
1536 |
* @see #toConcurrentMap(Function, Function) |
|
1537 |
* @see #toConcurrentMap(Function, Function, BinaryOperator, Supplier) |
|
1538 |
* @see #toMap(Function, Function, BinaryOperator) |
|
1539 |
*/ |
|
1540 |
public static <T, K, U> |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1541 |
Collector<T, ?, ConcurrentMap<K,U>> |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1542 |
toConcurrentMap(Function<? super T, ? extends K> keyMapper, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1543 |
Function<? super T, ? extends U> valueMapper, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1544 |
BinaryOperator<U> mergeFunction) { |
17196 | 1545 |
return toConcurrentMap(keyMapper, valueMapper, mergeFunction, ConcurrentHashMap::new); |
1546 |
} |
|
1547 |
||
1548 |
/** |
|
21339 | 1549 |
* Returns a concurrent {@code Collector} that accumulates elements into a |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1550 |
* {@code ConcurrentMap} whose keys and values are the result of applying |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1551 |
* the provided mapping functions to the input elements. |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1552 |
* |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1553 |
* <p>If the mapped keys contains duplicates (according to {@link Object#equals(Object)}), |
17196 | 1554 |
* the value mapping function is applied to each equal element, and the |
1555 |
* results are merged using the provided merging function. The |
|
1556 |
* {@code ConcurrentMap} is created by a provided supplier function. |
|
1557 |
* |
|
1558 |
* <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and |
|
1559 |
* {@link Collector.Characteristics#UNORDERED unordered} Collector. |
|
1560 |
* |
|
1561 |
* @param <T> the type of the input elements |
|
1562 |
* @param <K> the output type of the key mapping function |
|
1563 |
* @param <U> the output type of the value mapping function |
|
1564 |
* @param <M> the type of the resulting {@code ConcurrentMap} |
|
1565 |
* @param keyMapper a mapping function to produce keys |
|
1566 |
* @param valueMapper a mapping function to produce values |
|
1567 |
* @param mergeFunction a merge function, used to resolve collisions between |
|
1568 |
* values associated with the same key, as supplied |
|
1569 |
* to {@link Map#merge(Object, Object, BiFunction)} |
|
1570 |
* @param mapSupplier a function which returns a new, empty {@code Map} into |
|
1571 |
* which the results will be inserted |
|
21339 | 1572 |
* @return a concurrent, unordered {@code Collector} which collects elements into a |
17196 | 1573 |
* {@code ConcurrentMap} whose keys are the result of applying a key mapping |
1574 |
* function to the input elements, and whose values are the result of |
|
1575 |
* applying a value mapping function to all input elements equal to the key |
|
1576 |
* and combining them using the merge function |
|
1577 |
* |
|
1578 |
* @see #toConcurrentMap(Function, Function) |
|
1579 |
* @see #toConcurrentMap(Function, Function, BinaryOperator) |
|
1580 |
* @see #toMap(Function, Function, BinaryOperator, Supplier) |
|
1581 |
*/ |
|
1582 |
public static <T, K, U, M extends ConcurrentMap<K, U>> |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1583 |
Collector<T, ?, M> toConcurrentMap(Function<? super T, ? extends K> keyMapper, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1584 |
Function<? super T, ? extends U> valueMapper, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1585 |
BinaryOperator<U> mergeFunction, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1586 |
Supplier<M> mapSupplier) { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1587 |
BiConsumer<M, T> accumulator |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1588 |
= (map, element) -> map.merge(keyMapper.apply(element), |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1589 |
valueMapper.apply(element), mergeFunction); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1590 |
return new CollectorImpl<>(mapSupplier, accumulator, mapMerger(mergeFunction), CH_CONCURRENT_ID); |
17196 | 1591 |
} |
1592 |
||
1593 |
/** |
|
1594 |
* Returns a {@code Collector} which applies an {@code int}-producing |
|
1595 |
* mapping function to each input element, and returns summary statistics |
|
1596 |
* for the resulting values. |
|
1597 |
* |
|
1598 |
* @param <T> the type of the input elements |
|
1599 |
* @param mapper a mapping function to apply to each element |
|
1600 |
* @return a {@code Collector} implementing the summary-statistics reduction |
|
1601 |
* |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1602 |
* @see #summarizingDouble(ToDoubleFunction) |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1603 |
* @see #summarizingLong(ToLongFunction) |
17196 | 1604 |
*/ |
1605 |
public static <T> |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1606 |
Collector<T, ?, IntSummaryStatistics> summarizingInt(ToIntFunction<? super T> mapper) { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1607 |
return new CollectorImpl<T, IntSummaryStatistics, IntSummaryStatistics>( |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1608 |
IntSummaryStatistics::new, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1609 |
(r, t) -> r.accept(mapper.applyAsInt(t)), |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1610 |
(l, r) -> { l.combine(r); return l; }, CH_ID); |
17196 | 1611 |
} |
1612 |
||
1613 |
/** |
|
1614 |
* Returns a {@code Collector} which applies an {@code long}-producing |
|
1615 |
* mapping function to each input element, and returns summary statistics |
|
1616 |
* for the resulting values. |
|
1617 |
* |
|
1618 |
* @param <T> the type of the input elements |
|
1619 |
* @param mapper the mapping function to apply to each element |
|
1620 |
* @return a {@code Collector} implementing the summary-statistics reduction |
|
1621 |
* |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1622 |
* @see #summarizingDouble(ToDoubleFunction) |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1623 |
* @see #summarizingInt(ToIntFunction) |
17196 | 1624 |
*/ |
1625 |
public static <T> |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1626 |
Collector<T, ?, LongSummaryStatistics> summarizingLong(ToLongFunction<? super T> mapper) { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1627 |
return new CollectorImpl<T, LongSummaryStatistics, LongSummaryStatistics>( |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1628 |
LongSummaryStatistics::new, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1629 |
(r, t) -> r.accept(mapper.applyAsLong(t)), |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1630 |
(l, r) -> { l.combine(r); return l; }, CH_ID); |
17196 | 1631 |
} |
1632 |
||
1633 |
/** |
|
1634 |
* Returns a {@code Collector} which applies an {@code double}-producing |
|
1635 |
* mapping function to each input element, and returns summary statistics |
|
1636 |
* for the resulting values. |
|
1637 |
* |
|
1638 |
* @param <T> the type of the input elements |
|
1639 |
* @param mapper a mapping function to apply to each element |
|
1640 |
* @return a {@code Collector} implementing the summary-statistics reduction |
|
1641 |
* |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1642 |
* @see #summarizingLong(ToLongFunction) |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1643 |
* @see #summarizingInt(ToIntFunction) |
17196 | 1644 |
*/ |
1645 |
public static <T> |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1646 |
Collector<T, ?, DoubleSummaryStatistics> summarizingDouble(ToDoubleFunction<? super T> mapper) { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1647 |
return new CollectorImpl<T, DoubleSummaryStatistics, DoubleSummaryStatistics>( |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1648 |
DoubleSummaryStatistics::new, |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1649 |
(r, t) -> r.accept(mapper.applyAsDouble(t)), |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1650 |
(l, r) -> { l.combine(r); return l; }, CH_ID); |
17196 | 1651 |
} |
1652 |
||
1653 |
/** |
|
1654 |
* Implementation class used by partitioningBy. |
|
1655 |
*/ |
|
1656 |
private static final class Partition<T> |
|
1657 |
extends AbstractMap<Boolean, T> |
|
1658 |
implements Map<Boolean, T> { |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1659 |
final T forTrue; |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1660 |
final T forFalse; |
17196 | 1661 |
|
1662 |
Partition(T forTrue, T forFalse) { |
|
1663 |
this.forTrue = forTrue; |
|
1664 |
this.forFalse = forFalse; |
|
1665 |
} |
|
1666 |
||
1667 |
@Override |
|
1668 |
public Set<Map.Entry<Boolean, T>> entrySet() { |
|
1669 |
return new AbstractSet<Map.Entry<Boolean, T>>() { |
|
1670 |
@Override |
|
1671 |
public Iterator<Map.Entry<Boolean, T>> iterator() { |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1672 |
Map.Entry<Boolean, T> falseEntry = new SimpleImmutableEntry<>(false, forFalse); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1673 |
Map.Entry<Boolean, T> trueEntry = new SimpleImmutableEntry<>(true, forTrue); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
1674 |
return Arrays.asList(falseEntry, trueEntry).iterator(); |
17196 | 1675 |
} |
1676 |
||
1677 |
@Override |
|
1678 |
public int size() { |
|
1679 |
return 2; |
|
1680 |
} |
|
1681 |
}; |
|
1682 |
} |
|
1683 |
} |
|
1684 |
} |