17167
|
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.Comparator;
|
|
28 |
import java.util.Optional;
|
|
29 |
import java.util.function.BiConsumer;
|
|
30 |
import java.util.function.BiFunction;
|
|
31 |
import java.util.function.BinaryOperator;
|
|
32 |
import java.util.function.Consumer;
|
|
33 |
import java.util.function.Function;
|
|
34 |
import java.util.function.IntFunction;
|
|
35 |
import java.util.function.Predicate;
|
|
36 |
import java.util.function.Supplier;
|
|
37 |
import java.util.function.ToDoubleFunction;
|
|
38 |
import java.util.function.ToIntFunction;
|
|
39 |
import java.util.function.ToLongFunction;
|
|
40 |
|
|
41 |
// @@@ Specification to-do list @@@
|
|
42 |
// - Describe the difference between sequential and parallel streams
|
|
43 |
// - More general information about reduce, better definitions for associativity, more description of
|
|
44 |
// how reduce employs parallelism, more examples
|
|
45 |
// - Role of stream flags in various operations, specifically ordering
|
|
46 |
// - Whether each op preserves encounter order
|
|
47 |
// @@@ Specification to-do list @@@
|
|
48 |
|
|
49 |
/**
|
|
50 |
* A sequence of elements supporting sequential and parallel bulk operations.
|
|
51 |
* Streams support lazy intermediate operations (transforming a stream to
|
|
52 |
* another stream) such as {@code filter} and {@code map}, and terminal
|
|
53 |
* operations (consuming the contents of a stream to produce a result or
|
|
54 |
* side-effect), such as {@code forEach}, {@code findFirst}, and {@code
|
|
55 |
* iterator}. Once an operation has been performed on a stream, it
|
|
56 |
* is considered <em>consumed</em> and no longer usable for other operations.
|
|
57 |
*
|
|
58 |
* <p>For sequential stream pipelines, all operations are performed in the
|
|
59 |
* <a href="package-summary.html#Ordering">encounter order</a> of the pipeline
|
|
60 |
* source, if the pipeline source has a defined encounter order.
|
|
61 |
*
|
|
62 |
* <p>For parallel stream pipelines, unless otherwise specified, intermediate
|
|
63 |
* stream operations preserve the <a href="package-summary.html#Ordering">
|
|
64 |
* encounter order</a> of their source, and terminal operations
|
|
65 |
* respect the encounter order of their source, if the source
|
|
66 |
* has an encounter order. Provided that and parameters to stream operations
|
|
67 |
* satisfy the <a href="package-summary.html#NonInterference">non-interference
|
|
68 |
* requirements</a>, and excepting differences arising from the absence of
|
|
69 |
* a defined encounter order, the result of a stream pipeline should be the
|
|
70 |
* stable across multiple executions of the same operations on the same source.
|
|
71 |
* However, the timing and thread in which side-effects occur (for those
|
|
72 |
* operations which are allowed to produce side-effects, such as
|
|
73 |
* {@link #forEach(Consumer)}), are explicitly nondeterministic for parallel
|
|
74 |
* execution of stream pipelines.
|
|
75 |
*
|
|
76 |
* <p>Unless otherwise noted, passing a {@code null} argument to any stream
|
|
77 |
* method may result in a {@link NullPointerException}.
|
|
78 |
*
|
|
79 |
* @apiNote
|
|
80 |
* Streams are not data structures; they do not manage the storage for their
|
|
81 |
* elements, nor do they support access to individual elements. However,
|
|
82 |
* you can use the {@link #iterator()} or {@link #spliterator()} operations to
|
|
83 |
* perform a controlled traversal.
|
|
84 |
*
|
|
85 |
* @param <T> type of elements
|
|
86 |
* @since 1.8
|
|
87 |
* @see <a href="package-summary.html">java.util.stream</a>
|
|
88 |
*/
|
|
89 |
public interface Stream<T> extends BaseStream<T, Stream<T>> {
|
|
90 |
|
|
91 |
/**
|
|
92 |
* Returns a stream consisting of the elements of this stream that match
|
|
93 |
* the given predicate.
|
|
94 |
*
|
|
95 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate
|
|
96 |
* operation</a>.
|
|
97 |
*
|
|
98 |
* @param predicate a <a href="package-summary.html#NonInterference">
|
|
99 |
* non-interfering, stateless</a> predicate to apply to
|
|
100 |
* each element to determine if it should be included
|
|
101 |
* @return the new stream
|
|
102 |
*/
|
|
103 |
Stream<T> filter(Predicate<? super T> predicate);
|
|
104 |
|
|
105 |
/**
|
|
106 |
* Returns a stream consisting of the results of applying the given
|
|
107 |
* function to the elements of this stream.
|
|
108 |
*
|
|
109 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate
|
|
110 |
* operation</a>.
|
|
111 |
*
|
|
112 |
* @param <R> The element type of the new stream
|
|
113 |
* @param mapper a <a href="package-summary.html#NonInterference">
|
|
114 |
* non-interfering, stateless</a> function to apply to each
|
|
115 |
* element
|
|
116 |
* @return the new stream
|
|
117 |
*/
|
|
118 |
<R> Stream<R> map(Function<? super T, ? extends R> mapper);
|
|
119 |
|
|
120 |
/**
|
|
121 |
* Returns an {@code IntStream} consisting of the results of applying the
|
|
122 |
* given function to the elements of this stream.
|
|
123 |
*
|
|
124 |
* <p>This is an <a href="package-summary.html#StreamOps">
|
|
125 |
* intermediate operation</a>.
|
|
126 |
*
|
|
127 |
* @param mapper a <a href="package-summary.html#NonInterference">
|
|
128 |
* non-interfering, stateless</a> function to apply to each
|
|
129 |
* element
|
|
130 |
* @return the new stream
|
|
131 |
*/
|
|
132 |
IntStream mapToInt(ToIntFunction<? super T> mapper);
|
|
133 |
|
|
134 |
/**
|
|
135 |
* Returns a {@code LongStream} consisting of the results of applying the
|
|
136 |
* given function to the elements of this stream.
|
|
137 |
*
|
|
138 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate
|
|
139 |
* operation</a>.
|
|
140 |
*
|
|
141 |
* @param mapper a <a href="package-summary.html#NonInterference">
|
|
142 |
* non-interfering, stateless</a> function to apply to each
|
|
143 |
* element
|
|
144 |
* @return the new stream
|
|
145 |
*/
|
|
146 |
LongStream mapToLong(ToLongFunction<? super T> mapper);
|
|
147 |
|
|
148 |
/**
|
|
149 |
* Returns a {@code DoubleStream} consisting of the results of applying the
|
|
150 |
* given function to the elements of this stream.
|
|
151 |
*
|
|
152 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate
|
|
153 |
* operation</a>.
|
|
154 |
*
|
|
155 |
* @param mapper a <a href="package-summary.html#NonInterference">
|
|
156 |
* non-interfering, stateless</a> function to apply to each
|
|
157 |
* element
|
|
158 |
* @return the new stream
|
|
159 |
*/
|
|
160 |
DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper);
|
|
161 |
|
|
162 |
/**
|
|
163 |
* Returns a stream consisting of the results of replacing each element of
|
|
164 |
* this stream with the contents of the stream produced by applying the
|
|
165 |
* provided mapping function to each element. If the result of the mapping
|
|
166 |
* function is {@code null}, this is treated as if the result is an empty
|
|
167 |
* stream.
|
|
168 |
*
|
|
169 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate
|
|
170 |
* operation</a>.
|
|
171 |
*
|
|
172 |
* @apiNote
|
|
173 |
* The {@code flatMap()} operation has the effect of applying a one-to-many
|
|
174 |
* tranformation to the elements of the stream, and then flattening the
|
|
175 |
* resulting elements into a new stream. For example, if {@code orders}
|
|
176 |
* is a stream of purchase orders, and each purchase order contains a
|
|
177 |
* collection of line items, then the following produces a stream of line
|
|
178 |
* items:
|
|
179 |
* <pre>{@code
|
|
180 |
* orderStream.flatMap(order -> order.getLineItems().stream())...
|
|
181 |
* }</pre>
|
|
182 |
*
|
|
183 |
* @param <R> The element type of the new stream
|
|
184 |
* @param mapper a <a href="package-summary.html#NonInterference">
|
|
185 |
* non-interfering, stateless</a> function to apply to each
|
|
186 |
* element which produces a stream of new values
|
|
187 |
* @return the new stream
|
|
188 |
*/
|
|
189 |
<R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper);
|
|
190 |
|
|
191 |
/**
|
|
192 |
* Returns an {@code IntStream} consisting of the results of replacing each
|
|
193 |
* element of this stream with the contents of the stream produced by
|
|
194 |
* applying the provided mapping function to each element. If the result of
|
|
195 |
* the mapping function is {@code null}, this is treated as if the result is
|
|
196 |
* an empty stream.
|
|
197 |
*
|
|
198 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate
|
|
199 |
* operation</a>.
|
|
200 |
*
|
|
201 |
* @param mapper a <a href="package-summary.html#NonInterference">
|
|
202 |
* non-interfering, stateless</a> function to apply to each
|
|
203 |
* element which produces a stream of new values
|
|
204 |
* @return the new stream
|
|
205 |
*/
|
|
206 |
IntStream flatMapToInt(Function<? super T, ? extends IntStream> mapper);
|
|
207 |
|
|
208 |
/**
|
|
209 |
* Returns a {@code LongStream} consisting of the results of replacing each
|
|
210 |
* element of this stream with the contents of the stream produced
|
|
211 |
* by applying the provided mapping function to each element. If the result
|
|
212 |
* of the mapping function is {@code null}, this is treated as if the
|
|
213 |
* result is an empty stream.
|
|
214 |
*
|
|
215 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate
|
|
216 |
* operation</a>.
|
|
217 |
*
|
|
218 |
* @param mapper a <a href="package-summary.html#NonInterference">
|
|
219 |
* non-interfering, stateless</a> function to apply to
|
|
220 |
* each element which produces a stream of new values
|
|
221 |
* @return the new stream
|
|
222 |
*/
|
|
223 |
LongStream flatMapToLong(Function<? super T, ? extends LongStream> mapper);
|
|
224 |
|
|
225 |
/**
|
|
226 |
* Returns a {@code DoubleStream} consisting of the results of replacing each
|
|
227 |
* element of this stream with the contents of the stream produced
|
|
228 |
* by applying the provided mapping function to each element. If the result
|
|
229 |
* of the mapping function is {@code null}, this is treated as if the result
|
|
230 |
* is an empty stream.
|
|
231 |
*
|
|
232 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate
|
|
233 |
* operation</a>.
|
|
234 |
*
|
|
235 |
* @param mapper a <a href="package-summary.html#NonInterference">
|
|
236 |
* non-interfering, stateless</a> function to apply to each
|
|
237 |
* element which produces a stream of new values
|
|
238 |
* @return the new stream
|
|
239 |
*/
|
|
240 |
DoubleStream flatMapToDouble(Function<? super T, ? extends DoubleStream> mapper);
|
|
241 |
|
|
242 |
/**
|
|
243 |
* Returns a stream consisting of the distinct elements (according to
|
|
244 |
* {@link Object#equals(Object)}) of this stream.
|
|
245 |
*
|
|
246 |
* <p>This is a <a href="package-summary.html#StreamOps">stateful
|
|
247 |
* intermediate operation</a>.
|
|
248 |
*
|
|
249 |
* @return the new stream
|
|
250 |
*/
|
|
251 |
Stream<T> distinct();
|
|
252 |
|
|
253 |
/**
|
|
254 |
* Returns a stream consisting of the elements of this stream, sorted
|
|
255 |
* according to natural order. If the elements of this stream are not
|
|
256 |
* {@code Comparable}, a {@code java.lang.ClassCastException} may be thrown
|
|
257 |
* when the stream pipeline is executed.
|
|
258 |
*
|
|
259 |
* <p>This is a <a href="package-summary.html#StreamOps">stateful
|
|
260 |
* intermediate operation</a>.
|
|
261 |
*
|
|
262 |
* @return the new stream
|
|
263 |
*/
|
|
264 |
Stream<T> sorted();
|
|
265 |
|
|
266 |
/**
|
|
267 |
* Returns a stream consisting of the elements of this stream, sorted
|
|
268 |
* according to the provided {@code Comparator}.
|
|
269 |
*
|
|
270 |
* <p>This is a <a href="package-summary.html#StreamOps">stateful
|
|
271 |
* intermediate operation</a>.
|
|
272 |
*
|
|
273 |
* @param comparator a <a href="package-summary.html#NonInterference">
|
|
274 |
* non-interfering, stateless</a> {@code Comparator} to
|
|
275 |
* be used to compare stream elements
|
|
276 |
* @return the new stream
|
|
277 |
*/
|
|
278 |
Stream<T> sorted(Comparator<? super T> comparator);
|
|
279 |
|
|
280 |
/**
|
|
281 |
* Returns a stream consisting of the elements of this stream, additionally
|
|
282 |
* performing the provided action on each element as elements are consumed
|
|
283 |
* from the resulting stream.
|
|
284 |
*
|
|
285 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate
|
|
286 |
* operation</a>.
|
|
287 |
*
|
|
288 |
* <p>For parallel stream pipelines, the action may be called at
|
|
289 |
* whatever time and in whatever thread the element is made available by the
|
|
290 |
* upstream operation. If the action modifies shared state,
|
|
291 |
* it is responsible for providing the required synchronization.
|
|
292 |
*
|
|
293 |
* @apiNote This method exists mainly to support debugging, where you want
|
|
294 |
* to see the elements as they flow past a certain point in a pipeline:
|
|
295 |
* <pre>{@code
|
|
296 |
* list.stream()
|
|
297 |
* .filter(filteringFunction)
|
|
298 |
* .peek(e -> {System.out.println("Filtered value: " + e); });
|
|
299 |
* .map(mappingFunction)
|
|
300 |
* .peek(e -> {System.out.println("Mapped value: " + e); });
|
|
301 |
* .collect(Collectors.intoList());
|
|
302 |
* }</pre>
|
|
303 |
*
|
|
304 |
* @param consumer a <a href="package-summary.html#NonInterference">
|
|
305 |
* non-interfering</a> action to perform on the elements as
|
|
306 |
* they are consumed from the stream
|
|
307 |
* @return the new stream
|
|
308 |
*/
|
|
309 |
Stream<T> peek(Consumer<? super T> consumer);
|
|
310 |
|
|
311 |
/**
|
|
312 |
* Returns a stream consisting of the elements of this stream, truncated
|
|
313 |
* to be no longer than {@code maxSize} in length.
|
|
314 |
*
|
|
315 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
|
|
316 |
* stateful intermediate operation</a>.
|
|
317 |
*
|
|
318 |
* @param maxSize the number of elements the stream should be limited to
|
|
319 |
* @return the new stream
|
|
320 |
* @throws IllegalArgumentException if {@code maxSize} is negative
|
|
321 |
*/
|
|
322 |
Stream<T> limit(long maxSize);
|
|
323 |
|
|
324 |
/**
|
|
325 |
* Returns a stream consisting of the remaining elements of this stream
|
|
326 |
* after indexing {@code startInclusive} elements into the stream. If the
|
|
327 |
* {@code startInclusive} index lies past the end of this stream then an
|
|
328 |
* empty stream will be returned.
|
|
329 |
*
|
|
330 |
* <p>This is a <a href="package-summary.html#StreamOps">stateful
|
|
331 |
* intermediate operation</a>.
|
|
332 |
*
|
|
333 |
* @param startInclusive the number of leading elements to skip
|
|
334 |
* @return the new stream
|
|
335 |
* @throws IllegalArgumentException if {@code startInclusive} is negative
|
|
336 |
*/
|
|
337 |
Stream<T> substream(long startInclusive);
|
|
338 |
|
|
339 |
/**
|
|
340 |
* Returns a stream consisting of the remaining elements of this stream
|
|
341 |
* after indexing {@code startInclusive} elements into the stream and
|
|
342 |
* truncated to contain no more than {@code endExclusive - startInclusive}
|
|
343 |
* elements. If the {@code startInclusive} index lies past the end
|
|
344 |
* of this stream then an empty stream will be returned.
|
|
345 |
*
|
|
346 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
|
|
347 |
* stateful intermediate operation</a>.
|
|
348 |
*
|
|
349 |
* @param startInclusive the starting position of the substream, inclusive
|
|
350 |
* @param endExclusive the ending position of the substream, exclusive
|
|
351 |
* @return the new stream
|
|
352 |
* @throws IllegalArgumentException if {@code startInclusive} or
|
|
353 |
* {@code endExclusive} is negative or {@code startInclusive} is greater
|
|
354 |
* than {@code endExclusive}
|
|
355 |
*/
|
|
356 |
Stream<T> substream(long startInclusive, long endExclusive);
|
|
357 |
|
|
358 |
/**
|
|
359 |
* Performs an action for each element of this stream.
|
|
360 |
*
|
|
361 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal
|
|
362 |
* operation</a>.
|
|
363 |
*
|
|
364 |
* <p>For parallel stream pipelines, this operation does <em>not</em>
|
|
365 |
* guarantee to respect the encounter order of the stream, as doing so
|
|
366 |
* would sacrifice the benefit of parallelism. For any given element, the
|
|
367 |
* action may be performed at whatever time and in whatever thread the
|
|
368 |
* library chooses. If the action accesses shared state, it is
|
|
369 |
* responsible for providing the required synchronization.
|
|
370 |
*
|
|
371 |
* @param action a <a href="package-summary.html#NonInterference">
|
|
372 |
* non-interfering</a> action to perform on the elements
|
|
373 |
*/
|
|
374 |
void forEach(Consumer<? super T> action);
|
|
375 |
|
|
376 |
/**
|
|
377 |
* Performs an action for each element of this stream, guaranteeing that
|
|
378 |
* each element is processed in encounter order for streams that have a
|
|
379 |
* defined encounter order.
|
|
380 |
*
|
|
381 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal
|
|
382 |
* operation</a>.
|
|
383 |
*
|
|
384 |
* @param action a <a href="package-summary.html#NonInterference">
|
|
385 |
* non-interfering</a> action to perform on the elements
|
|
386 |
* @see #forEach(Consumer)
|
|
387 |
*/
|
|
388 |
void forEachOrdered(Consumer<? super T> action);
|
|
389 |
|
|
390 |
/**
|
|
391 |
* Returns an array containing the elements of this stream.
|
|
392 |
*
|
|
393 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal
|
|
394 |
* operation</a>.
|
|
395 |
*
|
|
396 |
* @return an array containing the elements of this stream
|
|
397 |
*/
|
|
398 |
Object[] toArray();
|
|
399 |
|
|
400 |
/**
|
|
401 |
* Returns an array containing the elements of this stream, using the
|
|
402 |
* provided {@code generator} function to allocate the returned array.
|
|
403 |
*
|
|
404 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal
|
|
405 |
* operation</a>.
|
|
406 |
*
|
|
407 |
* @param <A> the element type of the resulting array
|
|
408 |
* @param generator a function which produces a new array of the desired
|
|
409 |
* type and the provided length
|
|
410 |
* @return an array containing the elements in this stream
|
|
411 |
* @throws ArrayStoreException if the runtime type of the array returned
|
|
412 |
* from the array generator is not a supertype of the runtime type of every
|
|
413 |
* element in this stream
|
|
414 |
*/
|
|
415 |
<A> A[] toArray(IntFunction<A[]> generator);
|
|
416 |
|
|
417 |
/**
|
|
418 |
* Performs a <a href="package-summary.html#Reduction">reduction</a> on the
|
|
419 |
* elements of this stream, using the provided identity value and an
|
|
420 |
* <a href="package-summary.html#Associativity">associative</a>
|
|
421 |
* accumulation function, and returns the reduced value. This is equivalent
|
|
422 |
* to:
|
|
423 |
* <pre>{@code
|
|
424 |
* T result = identity;
|
|
425 |
* for (T element : this stream)
|
|
426 |
* result = accumulator.apply(result, element)
|
|
427 |
* return result;
|
|
428 |
* }</pre>
|
|
429 |
*
|
|
430 |
* but is not constrained to execute sequentially.
|
|
431 |
*
|
|
432 |
* <p>The {@code identity} value must be an identity for the accumulator
|
|
433 |
* function. This means that for all {@code t},
|
|
434 |
* {@code accumulator.apply(identity, t)} is equal to {@code t}.
|
|
435 |
* The {@code accumulator} function must be an
|
|
436 |
* <a href="package-summary.html#Associativity">associative</a> function.
|
|
437 |
*
|
|
438 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal
|
|
439 |
* operation</a>.
|
|
440 |
*
|
|
441 |
* @apiNote Sum, min, max, average, and string concatenation are all special
|
|
442 |
* cases of reduction. Summing a stream of numbers can be expressed as:
|
|
443 |
*
|
|
444 |
* <pre>{@code
|
|
445 |
* Integer sum = integers.reduce(0, (a, b) -> a+b);
|
|
446 |
* }</pre>
|
|
447 |
*
|
|
448 |
* or more compactly:
|
|
449 |
*
|
|
450 |
* <pre>{@code
|
|
451 |
* Integer sum = integers.reduce(0, Integer::sum);
|
|
452 |
* }</pre>
|
|
453 |
*
|
|
454 |
* <p>While this may seem a more roundabout way to perform an aggregation
|
|
455 |
* compared to simply mutating a running total in a loop, reduction
|
|
456 |
* operations parallelize more gracefully, without needing additional
|
|
457 |
* synchronization and with greatly reduced risk of data races.
|
|
458 |
*
|
|
459 |
* @param identity the identity value for the accumulating function
|
|
460 |
* @param accumulator an <a href="package-summary.html#Associativity">associative</a>
|
|
461 |
* <a href="package-summary.html#NonInterference">non-interfering,
|
|
462 |
* stateless</a> function for combining two values
|
|
463 |
* @return the result of the reduction
|
|
464 |
*/
|
|
465 |
T reduce(T identity, BinaryOperator<T> accumulator);
|
|
466 |
|
|
467 |
/**
|
|
468 |
* Performs a <a href="package-summary.html#Reduction">reduction</a> on the
|
|
469 |
* elements of this stream, using an
|
|
470 |
* <a href="package-summary.html#Associativity">associative</a> accumulation
|
|
471 |
* function, and returns an {@code Optional} describing the reduced value,
|
|
472 |
* if any. This is equivalent to:
|
|
473 |
* <pre>{@code
|
|
474 |
* boolean foundAny = false;
|
|
475 |
* T result = null;
|
|
476 |
* for (T element : this stream) {
|
|
477 |
* if (!foundAny) {
|
|
478 |
* foundAny = true;
|
|
479 |
* result = element;
|
|
480 |
* }
|
|
481 |
* else
|
|
482 |
* result = accumulator.apply(result, element);
|
|
483 |
* }
|
|
484 |
* return foundAny ? Optional.of(result) : Optional.empty();
|
|
485 |
* }</pre>
|
|
486 |
*
|
|
487 |
* but is not constrained to execute sequentially.
|
|
488 |
*
|
|
489 |
* <p>The {@code accumulator} function must be an
|
|
490 |
* <a href="package-summary.html#Associativity">associative</a> function.
|
|
491 |
*
|
|
492 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal
|
|
493 |
* operation</a>.
|
|
494 |
*
|
|
495 |
* @param accumulator an <a href="package-summary.html#Associativity">associative</a>
|
|
496 |
* <a href="package-summary.html#NonInterference">non-interfering,
|
|
497 |
* stateless</a> function for combining two values
|
|
498 |
* @return the result of the reduction
|
|
499 |
* @see #reduce(Object, BinaryOperator)
|
|
500 |
* @see #min(java.util.Comparator)
|
|
501 |
* @see #max(java.util.Comparator)
|
|
502 |
*/
|
|
503 |
Optional<T> reduce(BinaryOperator<T> accumulator);
|
|
504 |
|
|
505 |
/**
|
|
506 |
* Performs a <a href="package-summary.html#Reduction">reduction</a> on the
|
|
507 |
* elements of this stream, using the provided identity, accumulation
|
|
508 |
* function, and a combining functions. This is equivalent to:
|
|
509 |
* <pre>{@code
|
|
510 |
* U result = identity;
|
|
511 |
* for (T element : this stream)
|
|
512 |
* result = accumulator.apply(result, element)
|
|
513 |
* return result;
|
|
514 |
* }</pre>
|
|
515 |
*
|
|
516 |
* but is not constrained to execute sequentially.
|
|
517 |
*
|
|
518 |
* <p>The {@code identity} value must be an identity for the combiner
|
|
519 |
* function. This means that for all {@code u}, {@code combiner(identity, u)}
|
|
520 |
* is equal to {@code u}. Additionally, the {@code combiner} function
|
|
521 |
* must be compatible with the {@code accumulator} function; for all
|
|
522 |
* {@code u} and {@code t}, the following must hold:
|
|
523 |
* <pre>{@code
|
|
524 |
* combiner.apply(u, accumulator.apply(identity, t)) == accumulator.apply(u, t)
|
|
525 |
* }</pre>
|
|
526 |
*
|
|
527 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal
|
|
528 |
* operation</a>.
|
|
529 |
*
|
|
530 |
* @apiNote Many reductions using this form can be represented more simply
|
|
531 |
* by an explicit combination of {@code map} and {@code reduce} operations.
|
|
532 |
* The {@code accumulator} function acts as a fused mapper and accumulator,
|
|
533 |
* which can sometimes be more efficient than separate mapping and reduction,
|
|
534 |
* such as in the case where knowing the previously reduced value allows you
|
|
535 |
* to avoid some computation.
|
|
536 |
*
|
|
537 |
* @param <U> The type of the result
|
|
538 |
* @param identity the identity value for the combiner function
|
|
539 |
* @param accumulator an <a href="package-summary.html#Associativity">associative</a>
|
|
540 |
* <a href="package-summary.html#NonInterference">non-interfering,
|
|
541 |
* stateless</a> function for incorporating an additional
|
|
542 |
* element into a result
|
|
543 |
* @param combiner an <a href="package-summary.html#Associativity">associative</a>
|
|
544 |
* <a href="package-summary.html#NonInterference">non-interfering,
|
|
545 |
* stateless</a> function for combining two values, which
|
|
546 |
* must be compatible with the accumulator function
|
|
547 |
* @return the result of the reduction
|
|
548 |
* @see #reduce(BinaryOperator)
|
|
549 |
* @see #reduce(Object, BinaryOperator)
|
|
550 |
*/
|
|
551 |
<U> U reduce(U identity,
|
|
552 |
BiFunction<U, ? super T, U> accumulator,
|
|
553 |
BinaryOperator<U> combiner);
|
|
554 |
|
|
555 |
/**
|
|
556 |
* Performs a <a href="package-summary.html#MutableReduction">mutable
|
|
557 |
* reduction</a> operation on the elements of this stream. A mutable
|
|
558 |
* reduction is one in which the reduced value is a mutable value holder,
|
|
559 |
* such as an {@code ArrayList}, and elements are incorporated by updating
|
|
560 |
* the state of the result, rather than by replacing the result. This
|
|
561 |
* produces a result equivalent to:
|
|
562 |
* <pre>{@code
|
|
563 |
* R result = resultFactory.get();
|
|
564 |
* for (T element : this stream)
|
|
565 |
* accumulator.accept(result, element);
|
|
566 |
* return result;
|
|
567 |
* }</pre>
|
|
568 |
*
|
|
569 |
* <p>Like {@link #reduce(Object, BinaryOperator)}, {@code collect} operations
|
|
570 |
* can be parallelized without requiring additional synchronization.
|
|
571 |
*
|
|
572 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal
|
|
573 |
* operation</a>.
|
|
574 |
*
|
|
575 |
* @apiNote There are many existing classes in the JDK whose signatures are
|
|
576 |
* a good match for use as arguments to {@code collect()}. For example,
|
|
577 |
* the following will accumulate strings into an ArrayList:
|
|
578 |
* <pre>{@code
|
|
579 |
* List<String> asList = stringStream.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
|
|
580 |
* }</pre>
|
|
581 |
*
|
|
582 |
* <p>The following will take a stream of strings and concatenates them into a
|
|
583 |
* single string:
|
|
584 |
* <pre>{@code
|
|
585 |
* String concat = stringStream.collect(StringBuilder::new, StringBuilder::append,
|
|
586 |
* StringBuilder::append)
|
|
587 |
* .toString();
|
|
588 |
* }</pre>
|
|
589 |
*
|
|
590 |
* @param <R> type of the result
|
|
591 |
* @param resultFactory a function that creates a new result container.
|
|
592 |
* For a parallel execution, this function may be
|
|
593 |
* called multiple times and must return a fresh value
|
|
594 |
* each time.
|
|
595 |
* @param accumulator an <a href="package-summary.html#Associativity">associative</a>
|
|
596 |
* <a href="package-summary.html#NonInterference">non-interfering,
|
|
597 |
* stateless</a> function for incorporating an additional
|
|
598 |
* element into a result
|
|
599 |
* @param combiner an <a href="package-summary.html#Associativity">associative</a>
|
|
600 |
* <a href="package-summary.html#NonInterference">non-interfering,
|
|
601 |
* stateless</a> function for combining two values, which
|
|
602 |
* must be compatible with the accumulator function
|
|
603 |
* @return the result of the reduction
|
|
604 |
*/
|
|
605 |
<R> R collect(Supplier<R> resultFactory,
|
|
606 |
BiConsumer<R, ? super T> accumulator,
|
|
607 |
BiConsumer<R, R> combiner);
|
|
608 |
|
|
609 |
/**
|
|
610 |
* Performs a <a href="package-summary.html#MutableReduction">mutable
|
|
611 |
* reduction</a> operation on the elements of this stream using a
|
|
612 |
* {@code Collector} object to describe the reduction. A {@code Collector}
|
|
613 |
* encapsulates the functions used as arguments to
|
|
614 |
* {@link #collect(Supplier, BiConsumer, BiConsumer)}, allowing for reuse of
|
|
615 |
* collection strategies, and composition of collect operations such as
|
|
616 |
* multiple-level grouping or partitioning.
|
|
617 |
*
|
|
618 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal
|
|
619 |
* operation</a>.
|
|
620 |
*
|
|
621 |
* <p>When executed in parallel, multiple intermediate results may be
|
|
622 |
* instantiated, populated, and merged, so as to maintain isolation of
|
|
623 |
* mutable data structures. Therefore, even when executed in parallel
|
|
624 |
* with non-thread-safe data structures (such as {@code ArrayList}), no
|
|
625 |
* additional synchronization is needed for a parallel reduction.
|
|
626 |
*
|
|
627 |
* @apiNote
|
|
628 |
* The following will accumulate strings into an ArrayList:
|
|
629 |
* <pre>{@code
|
|
630 |
* List<String> asList = stringStream.collect(Collectors.toList());
|
|
631 |
* }</pre>
|
|
632 |
*
|
|
633 |
* <p>The following will classify {@code Person} objects by city:
|
|
634 |
* <pre>{@code
|
|
635 |
* Map<String, Collection<Person>> peopleByCity
|
|
636 |
* = personStream.collect(Collectors.groupBy(Person::getCity));
|
|
637 |
* }</pre>
|
|
638 |
*
|
|
639 |
* <p>The following will classify {@code Person} objects by state and city,
|
|
640 |
* cascading two {@code Collector}s together:
|
|
641 |
* <pre>{@code
|
|
642 |
* Map<String, Map<String, Collection<Person>>> peopleByStateAndCity
|
|
643 |
* = personStream.collect(Collectors.groupBy(Person::getState,
|
|
644 |
* Collectors.groupBy(Person::getCity)));
|
|
645 |
* }</pre>
|
|
646 |
*
|
|
647 |
* @param <R> the type of the result
|
|
648 |
* @param collector the {@code Collector} describing the reduction
|
|
649 |
* @return the result of the reduction
|
|
650 |
* @see #collect(Supplier, BiConsumer, BiConsumer)
|
|
651 |
* @see Collectors
|
|
652 |
*/
|
|
653 |
<R> R collect(Collector<? super T, R> collector);
|
|
654 |
|
|
655 |
/**
|
|
656 |
* Returns the minimum element of this stream according to the provided
|
|
657 |
* {@code Comparator}. This is a special case of a
|
|
658 |
* <a href="package-summary.html#MutableReduction">reduction</a>.
|
|
659 |
*
|
|
660 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>.
|
|
661 |
*
|
|
662 |
* @param comparator a <a href="package-summary.html#NonInterference">non-interfering,
|
|
663 |
* stateless</a> {@code Comparator} to use to compare
|
|
664 |
* elements of this stream
|
|
665 |
* @return an {@code Optional} describing the minimum element of this stream,
|
|
666 |
* or an empty {@code Optional} if the stream is empty
|
|
667 |
*/
|
|
668 |
Optional<T> min(Comparator<? super T> comparator);
|
|
669 |
|
|
670 |
/**
|
|
671 |
* Returns the maximum element of this stream according to the provided
|
|
672 |
* {@code Comparator}. This is a special case of a
|
|
673 |
* <a href="package-summary.html#MutableReduction">reduction</a>.
|
|
674 |
*
|
|
675 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal
|
|
676 |
* operation</a>.
|
|
677 |
*
|
|
678 |
* @param comparator a <a href="package-summary.html#NonInterference">non-interfering,
|
|
679 |
* stateless</a> {@code Comparator} to use to compare
|
|
680 |
* elements of this stream
|
|
681 |
* @return an {@code Optional} describing the maximum element of this stream,
|
|
682 |
* or an empty {@code Optional} if the stream is empty
|
|
683 |
*/
|
|
684 |
Optional<T> max(Comparator<? super T> comparator);
|
|
685 |
|
|
686 |
/**
|
|
687 |
* Returns the count of elements in this stream. This is a special case of
|
|
688 |
* a <a href="package-summary.html#MutableReduction">reduction</a> and is
|
|
689 |
* equivalent to:
|
|
690 |
* <pre>{@code
|
|
691 |
* return mapToLong(e -> 1L).sum();
|
|
692 |
* }</pre>
|
|
693 |
*
|
|
694 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>.
|
|
695 |
*
|
|
696 |
* @return the count of elements in this stream
|
|
697 |
*/
|
|
698 |
long count();
|
|
699 |
|
|
700 |
/**
|
|
701 |
* Returns whether any elements of this stream match the provided
|
|
702 |
* predicate. May not evaluate the predicate on all elements if not
|
|
703 |
* necessary for determining the result.
|
|
704 |
*
|
|
705 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
|
|
706 |
* terminal operation</a>.
|
|
707 |
*
|
|
708 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering,
|
|
709 |
* stateless</a> predicate to apply to elements of this
|
|
710 |
* stream
|
|
711 |
* @return {@code true} if any elements of the stream match the provided
|
|
712 |
* predicate otherwise {@code false}
|
|
713 |
*/
|
|
714 |
boolean anyMatch(Predicate<? super T> predicate);
|
|
715 |
|
|
716 |
/**
|
|
717 |
* Returns whether all elements of this stream match the provided predicate.
|
|
718 |
* May not evaluate the predicate on all elements if not necessary for
|
|
719 |
* determining the result.
|
|
720 |
*
|
|
721 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
|
|
722 |
* terminal operation</a>.
|
|
723 |
*
|
|
724 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering,
|
|
725 |
* stateless</a> predicate to apply to elements of this
|
|
726 |
* stream
|
|
727 |
* @return {@code true} if all elements of the stream match the provided
|
|
728 |
* predicate otherwise {@code false}
|
|
729 |
*/
|
|
730 |
boolean allMatch(Predicate<? super T> predicate);
|
|
731 |
|
|
732 |
/**
|
|
733 |
* Returns whether no elements of this stream match the provided predicate.
|
|
734 |
* May not evaluate the predicate on all elements if not necessary for
|
|
735 |
* determining the result.
|
|
736 |
*
|
|
737 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
|
|
738 |
* terminal operation</a>.
|
|
739 |
*
|
|
740 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering,
|
|
741 |
* stateless</a> predicate to apply to elements of this
|
|
742 |
* stream
|
|
743 |
* @return {@code true} if no elements of the stream match the provided
|
|
744 |
* predicate otherwise {@code false}
|
|
745 |
*/
|
|
746 |
boolean noneMatch(Predicate<? super T> predicate);
|
|
747 |
|
|
748 |
/**
|
|
749 |
* Returns an {@link Optional} describing the first element of this stream
|
|
750 |
* (in the encounter order), or an empty {@code Optional} if the stream is
|
|
751 |
* empty. If the stream has no encounter order, than any element may be
|
|
752 |
* returned.
|
|
753 |
*
|
|
754 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
|
|
755 |
* terminal operation</a>.
|
|
756 |
*
|
|
757 |
* @return an {@code Optional} describing the first element of this stream,
|
|
758 |
* or an empty {@code Optional} if the stream is empty
|
|
759 |
* @throws NullPointerException if the element selected is null
|
|
760 |
*/
|
|
761 |
Optional<T> findFirst();
|
|
762 |
|
|
763 |
/**
|
|
764 |
* Returns an {@link Optional} describing some element of the stream, or an
|
|
765 |
* empty {@code Optional} if the stream is empty.
|
|
766 |
*
|
|
767 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
|
|
768 |
* terminal operation</a>.
|
|
769 |
*
|
|
770 |
* <p>The behavior of this operation is explicitly nondeterministic; it is
|
|
771 |
* free to select any element in the stream. This is to allow for maximal
|
|
772 |
* performance in parallel operations; the cost is that multiple invocations
|
|
773 |
* on the same source may not return the same result. (If the first element
|
|
774 |
* in the encounter order is desired, use {@link #findFirst()} instead.)
|
|
775 |
*
|
|
776 |
* @return an {@code Optional} describing some element of this stream, or an
|
|
777 |
* empty {@code Optional} if the stream is empty
|
|
778 |
* @throws NullPointerException if the element selected is null
|
|
779 |
* @see #findFirst()
|
|
780 |
*/
|
|
781 |
Optional<T> findAny();
|
|
782 |
}
|