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