author | psandoz |
Thu, 16 Jan 2014 18:20:31 +0100 | |
changeset 22289 | bb9c71b84919 |
parent 21846 | c10feb34bc0b |
child 22352 | ecbf37860ffa |
permissions | -rw-r--r-- |
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 |
||
19850 | 27 |
import java.nio.charset.Charset; |
28 |
import java.nio.file.Files; |
|
29 |
import java.nio.file.Path; |
|
17195 | 30 |
import java.util.Arrays; |
19850 | 31 |
import java.util.Collection; |
17167 | 32 |
import java.util.Comparator; |
17195 | 33 |
import java.util.Iterator; |
34 |
import java.util.Objects; |
|
17167 | 35 |
import java.util.Optional; |
17195 | 36 |
import java.util.Spliterator; |
37 |
import java.util.Spliterators; |
|
19850 | 38 |
import java.util.concurrent.ConcurrentHashMap; |
17167 | 39 |
import java.util.function.BiConsumer; |
40 |
import java.util.function.BiFunction; |
|
41 |
import java.util.function.BinaryOperator; |
|
42 |
import java.util.function.Consumer; |
|
43 |
import java.util.function.Function; |
|
44 |
import java.util.function.IntFunction; |
|
45 |
import java.util.function.Predicate; |
|
46 |
import java.util.function.Supplier; |
|
47 |
import java.util.function.ToDoubleFunction; |
|
48 |
import java.util.function.ToIntFunction; |
|
49 |
import java.util.function.ToLongFunction; |
|
17195 | 50 |
import java.util.function.UnaryOperator; |
17167 | 51 |
|
52 |
/** |
|
19850 | 53 |
* A sequence of elements supporting sequential and parallel aggregate |
54 |
* operations. The following example illustrates an aggregate operation using |
|
55 |
* {@link Stream} and {@link IntStream}: |
|
56 |
* |
|
57 |
* <pre>{@code |
|
58 |
* int sum = widgets.stream() |
|
59 |
* .filter(w -> w.getColor() == RED) |
|
60 |
* .mapToInt(w -> w.getWeight()) |
|
61 |
* .sum(); |
|
62 |
* }</pre> |
|
63 |
* |
|
64 |
* In this example, {@code widgets} is a {@code Collection<Widget>}. We create |
|
65 |
* a stream of {@code Widget} objects via {@link Collection#stream Collection.stream()}, |
|
66 |
* filter it to produce a stream containing only the red widgets, and then |
|
67 |
* transform it into a stream of {@code int} values representing the weight of |
|
68 |
* each red widget. Then this stream is summed to produce a total weight. |
|
17167 | 69 |
* |
21339 | 70 |
* <p>In addition to {@code Stream}, which is a stream of object references, |
71 |
* there are primitive specializations for {@link IntStream}, {@link LongStream}, |
|
72 |
* and {@link DoubleStream}, all of which are referred to as "streams" and |
|
73 |
* conform to the characteristics and restrictions described here. |
|
74 |
* |
|
19850 | 75 |
* <p>To perform a computation, stream |
76 |
* <a href="package-summary.html#StreamOps">operations</a> are composed into a |
|
77 |
* <em>stream pipeline</em>. A stream pipeline consists of a source (which |
|
78 |
* might be an array, a collection, a generator function, an I/O channel, |
|
79 |
* etc), zero or more <em>intermediate operations</em> (which transform a |
|
80 |
* stream into another stream, such as {@link Stream#filter(Predicate)}), and a |
|
81 |
* <em>terminal operation</em> (which produces a result or side-effect, such |
|
82 |
* as {@link Stream#count()} or {@link Stream#forEach(Consumer)}). |
|
83 |
* Streams are lazy; computation on the source data is only performed when the |
|
84 |
* terminal operation is initiated, and source elements are consumed only |
|
85 |
* as needed. |
|
86 |
* |
|
87 |
* <p>Collections and streams, while bearing some superficial similarities, |
|
88 |
* have different goals. Collections are primarily concerned with the efficient |
|
89 |
* management of, and access to, their elements. By contrast, streams do not |
|
90 |
* provide a means to directly access or manipulate their elements, and are |
|
91 |
* instead concerned with declaratively describing their source and the |
|
92 |
* computational operations which will be performed in aggregate on that source. |
|
93 |
* However, if the provided stream operations do not offer the desired |
|
94 |
* functionality, the {@link #iterator()} and {@link #spliterator()} operations |
|
95 |
* can be used to perform a controlled traversal. |
|
17167 | 96 |
* |
19850 | 97 |
* <p>A stream pipeline, like the "widgets" example above, can be viewed as |
98 |
* a <em>query</em> on the stream source. Unless the source was explicitly |
|
99 |
* designed for concurrent modification (such as a {@link ConcurrentHashMap}), |
|
100 |
* unpredictable or erroneous behavior may result from modifying the stream |
|
101 |
* source while it is being queried. |
|
102 |
* |
|
103 |
* <p>Most stream operations accept parameters that describe user-specified |
|
104 |
* behavior, such as the lambda expression {@code w -> w.getWeight()} passed to |
|
21339 | 105 |
* {@code mapToInt} in the example above. To preserve correct behavior, |
106 |
* these <em>behavioral parameters</em>: |
|
107 |
* <ul> |
|
108 |
* <li>must be <a href="package-summary.html#NonInterference">non-interfering</a> |
|
109 |
* (they do not modify the stream source); and</li> |
|
110 |
* <li>in most cases must be <a href="package-summary.html#Statelessness">stateless</a> |
|
111 |
* (their result should not depend on any state that might change during execution |
|
112 |
* of the stream pipeline).</li> |
|
113 |
* </ul> |
|
114 |
* |
|
115 |
* <p>Such parameters are always instances of a |
|
116 |
* <a href="../function/package-summary.html">functional interface</a> such |
|
19850 | 117 |
* as {@link java.util.function.Function}, and are often lambda expressions or |
21339 | 118 |
* method references. Unless otherwise specified these parameters must be |
119 |
* <em>non-null</em>. |
|
17167 | 120 |
* |
19850 | 121 |
* <p>A stream should be operated on (invoking an intermediate or terminal stream |
122 |
* operation) only once. This rules out, for example, "forked" streams, where |
|
123 |
* the same source feeds two or more pipelines, or multiple traversals of the |
|
124 |
* same stream. A stream implementation may throw {@link IllegalStateException} |
|
125 |
* if it detects that the stream is being reused. However, since some stream |
|
126 |
* operations may return their receiver rather than a new stream object, it may |
|
127 |
* not be possible to detect reuse in all cases. |
|
17167 | 128 |
* |
19850 | 129 |
* <p>Streams have a {@link #close()} method and implement {@link AutoCloseable}, |
130 |
* but nearly all stream instances do not actually need to be closed after use. |
|
131 |
* Generally, only streams whose source is an IO channel (such as those returned |
|
132 |
* by {@link Files#lines(Path, Charset)}) will require closing. Most streams |
|
133 |
* are backed by collections, arrays, or generating functions, which require no |
|
134 |
* special resource management. (If a stream does require closing, it can be |
|
135 |
* declared as a resource in a {@code try}-with-resources statement.) |
|
17167 | 136 |
* |
19850 | 137 |
* <p>Stream pipelines may execute either sequentially or in |
138 |
* <a href="package-summary.html#Parallelism">parallel</a>. This |
|
139 |
* execution mode is a property of the stream. Streams are created |
|
140 |
* with an initial choice of sequential or parallel execution. (For example, |
|
141 |
* {@link Collection#stream() Collection.stream()} creates a sequential stream, |
|
142 |
* and {@link Collection#parallelStream() Collection.parallelStream()} creates |
|
143 |
* a parallel one.) This choice of execution mode may be modified by the |
|
144 |
* {@link #sequential()} or {@link #parallel()} methods, and may be queried with |
|
145 |
* the {@link #isParallel()} method. |
|
146 |
* |
|
147 |
* @param <T> the type of the stream elements |
|
17167 | 148 |
* @since 1.8 |
21339 | 149 |
* @see IntStream |
150 |
* @see LongStream |
|
151 |
* @see DoubleStream |
|
17167 | 152 |
* @see <a href="package-summary.html">java.util.stream</a> |
153 |
*/ |
|
154 |
public interface Stream<T> extends BaseStream<T, Stream<T>> { |
|
155 |
||
156 |
/** |
|
157 |
* Returns a stream consisting of the elements of this stream that match |
|
158 |
* the given predicate. |
|
159 |
* |
|
160 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
161 |
* operation</a>. |
|
162 |
* |
|
21339 | 163 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>, |
164 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
165 |
* predicate to apply to each element to determine if it |
|
166 |
* should be included |
|
17167 | 167 |
* @return the new stream |
168 |
*/ |
|
169 |
Stream<T> filter(Predicate<? super T> predicate); |
|
170 |
||
171 |
/** |
|
172 |
* Returns a stream consisting of the results of applying the given |
|
173 |
* function to the elements of this stream. |
|
174 |
* |
|
175 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
176 |
* operation</a>. |
|
177 |
* |
|
178 |
* @param <R> The element type of the new stream |
|
21339 | 179 |
* @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>, |
180 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
181 |
* function to apply to each element |
|
17167 | 182 |
* @return the new stream |
183 |
*/ |
|
184 |
<R> Stream<R> map(Function<? super T, ? extends R> mapper); |
|
185 |
||
186 |
/** |
|
187 |
* Returns an {@code IntStream} consisting of the results of applying the |
|
188 |
* given function to the elements of this stream. |
|
189 |
* |
|
190 |
* <p>This is an <a href="package-summary.html#StreamOps"> |
|
191 |
* intermediate operation</a>. |
|
192 |
* |
|
21339 | 193 |
* @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>, |
194 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
195 |
* function to apply to each element |
|
17167 | 196 |
* @return the new stream |
197 |
*/ |
|
198 |
IntStream mapToInt(ToIntFunction<? super T> mapper); |
|
199 |
||
200 |
/** |
|
201 |
* Returns a {@code LongStream} consisting of the results of applying the |
|
202 |
* given function to the elements of this stream. |
|
203 |
* |
|
204 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
205 |
* operation</a>. |
|
206 |
* |
|
21339 | 207 |
* @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>, |
208 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
209 |
* function to apply to each element |
|
17167 | 210 |
* @return the new stream |
211 |
*/ |
|
212 |
LongStream mapToLong(ToLongFunction<? super T> mapper); |
|
213 |
||
214 |
/** |
|
215 |
* Returns a {@code DoubleStream} consisting of the results of applying the |
|
216 |
* given function to the elements of this stream. |
|
217 |
* |
|
218 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
219 |
* operation</a>. |
|
220 |
* |
|
21339 | 221 |
* @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>, |
222 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
223 |
* function to apply to each element |
|
17167 | 224 |
* @return the new stream |
225 |
*/ |
|
226 |
DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper); |
|
227 |
||
228 |
/** |
|
229 |
* Returns a stream consisting of the results of replacing each element of |
|
230 |
* this stream with the contents of the stream produced by applying the |
|
19850 | 231 |
* provided mapping function to each element. (If the result of the mapping |
232 |
* function is {@code null}, this is treated as if the result was an empty |
|
233 |
* stream.) |
|
17167 | 234 |
* |
235 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
236 |
* operation</a>. |
|
237 |
* |
|
238 |
* @apiNote |
|
239 |
* The {@code flatMap()} operation has the effect of applying a one-to-many |
|
21339 | 240 |
* transformation to the elements of the stream, and then flattening the |
241 |
* resulting elements into a new stream. |
|
242 |
* |
|
243 |
* <p><b>Examples.</b> |
|
244 |
* |
|
245 |
* <p>If {@code orders} is a stream of purchase orders, and each purchase |
|
246 |
* order contains a collection of line items, then the following produces a |
|
247 |
* stream containing all the line items in all the orders: |
|
17167 | 248 |
* <pre>{@code |
21339 | 249 |
* orders.flatMap(order -> order.getLineItems().stream())... |
17167 | 250 |
* }</pre> |
251 |
* |
|
21339 | 252 |
* <p>If {@code path} is the path to a file, then the following produces a |
253 |
* stream of the {@code words} contained in that file: |
|
254 |
* <pre>{@code |
|
255 |
* Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8); |
|
256 |
* Stream<String> words = lines.flatMap(line -> Stream.of(line.split(" +"))); |
|
257 |
* }</pre> |
|
258 |
* The {@code mapper} function passed to {@code flatMap} splits a line, |
|
259 |
* using a simple regular expression, into an array of words, and then |
|
260 |
* creates a stream of words from that array. |
|
261 |
* |
|
17167 | 262 |
* @param <R> The element type of the new stream |
21339 | 263 |
* @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>, |
264 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
265 |
* function to apply to each element which produces a stream |
|
266 |
* of new values |
|
17167 | 267 |
* @return the new stream |
268 |
*/ |
|
269 |
<R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper); |
|
270 |
||
271 |
/** |
|
272 |
* Returns an {@code IntStream} consisting of the results of replacing each |
|
273 |
* element of this stream with the contents of the stream produced by |
|
19850 | 274 |
* applying the provided mapping function to each element. (If the result |
275 |
* of the mapping function is {@code null}, this is treated as if the result |
|
276 |
* was an empty stream.) |
|
17167 | 277 |
* |
278 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
279 |
* operation</a>. |
|
280 |
* |
|
21339 | 281 |
* @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>, |
282 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
283 |
* function to apply to each element which produces a stream |
|
284 |
* of new values |
|
17167 | 285 |
* @return the new stream |
21339 | 286 |
* @see #flatMap(Function) |
17167 | 287 |
*/ |
288 |
IntStream flatMapToInt(Function<? super T, ? extends IntStream> mapper); |
|
289 |
||
290 |
/** |
|
291 |
* Returns a {@code LongStream} consisting of the results of replacing each |
|
292 |
* element of this stream with the contents of the stream produced |
|
19850 | 293 |
* by applying the provided mapping function to each element. (If the result |
294 |
* of the mapping function is {@code null}, this is treated as if the result |
|
295 |
* was an empty stream.) |
|
17167 | 296 |
* |
297 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
298 |
* operation</a>. |
|
299 |
* |
|
21339 | 300 |
* @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>, |
301 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
302 |
* function to apply to each element which produces a stream |
|
303 |
* of new values |
|
17167 | 304 |
* @return the new stream |
21339 | 305 |
* @see #flatMap(Function) |
17167 | 306 |
*/ |
307 |
LongStream flatMapToLong(Function<? super T, ? extends LongStream> mapper); |
|
308 |
||
309 |
/** |
|
310 |
* Returns a {@code DoubleStream} consisting of the results of replacing each |
|
311 |
* element of this stream with the contents of the stream produced |
|
19850 | 312 |
* by applying the provided mapping function to each element. (If the result |
17167 | 313 |
* of the mapping function is {@code null}, this is treated as if the result |
19850 | 314 |
* was an empty stream.) |
17167 | 315 |
* |
316 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
317 |
* operation</a>. |
|
318 |
* |
|
21339 | 319 |
* @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>, |
320 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
321 |
* function to apply to each element which produces a stream |
|
322 |
* of new values |
|
17167 | 323 |
* @return the new stream |
21339 | 324 |
* @see #flatMap(Function) |
17167 | 325 |
*/ |
326 |
DoubleStream flatMapToDouble(Function<? super T, ? extends DoubleStream> mapper); |
|
327 |
||
328 |
/** |
|
329 |
* Returns a stream consisting of the distinct elements (according to |
|
330 |
* {@link Object#equals(Object)}) of this stream. |
|
331 |
* |
|
21339 | 332 |
* <p>For ordered streams, the selection of distinct elements is stable |
333 |
* (for duplicated elements, the element appearing first in the encounter |
|
334 |
* order is preserved.) For unordered streams, no stability guarantees |
|
335 |
* are made. |
|
336 |
* |
|
17167 | 337 |
* <p>This is a <a href="package-summary.html#StreamOps">stateful |
338 |
* intermediate operation</a>. |
|
339 |
* |
|
21339 | 340 |
* @apiNote |
341 |
* Preserving stability for {@code distinct()} in parallel pipelines is |
|
342 |
* relatively expensive (requires that the operation act as a full barrier, |
|
343 |
* with substantial buffering overhead), and stability is often not needed. |
|
344 |
* Using an unordered stream source (such as {@link #generate(Supplier)}) |
|
345 |
* or removing the ordering constraint with {@link #unordered()} may result |
|
346 |
* in significantly more efficient execution for {@code distinct()} in parallel |
|
347 |
* pipelines, if the semantics of your situation permit. If consistency |
|
348 |
* with encounter order is required, and you are experiencing poor performance |
|
349 |
* or memory utilization with {@code distinct()} in parallel pipelines, |
|
350 |
* switching to sequential execution with {@link #sequential()} may improve |
|
351 |
* performance. |
|
352 |
* |
|
17167 | 353 |
* @return the new stream |
354 |
*/ |
|
355 |
Stream<T> distinct(); |
|
356 |
||
357 |
/** |
|
358 |
* Returns a stream consisting of the elements of this stream, sorted |
|
359 |
* according to natural order. If the elements of this stream are not |
|
360 |
* {@code Comparable}, a {@code java.lang.ClassCastException} may be thrown |
|
19850 | 361 |
* when the terminal operation is executed. |
17167 | 362 |
* |
21339 | 363 |
* <p>For ordered streams, the sort is stable. For unordered streams, no |
364 |
* stability guarantees are made. |
|
365 |
* |
|
17167 | 366 |
* <p>This is a <a href="package-summary.html#StreamOps">stateful |
367 |
* intermediate operation</a>. |
|
368 |
* |
|
369 |
* @return the new stream |
|
370 |
*/ |
|
371 |
Stream<T> sorted(); |
|
372 |
||
373 |
/** |
|
374 |
* Returns a stream consisting of the elements of this stream, sorted |
|
375 |
* according to the provided {@code Comparator}. |
|
376 |
* |
|
21339 | 377 |
* <p>For ordered streams, the sort is stable. For unordered streams, no |
378 |
* stability guarantees are made. |
|
379 |
* |
|
17167 | 380 |
* <p>This is a <a href="package-summary.html#StreamOps">stateful |
381 |
* intermediate operation</a>. |
|
382 |
* |
|
21339 | 383 |
* @param comparator a <a href="package-summary.html#NonInterference">non-interfering</a>, |
384 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
385 |
* {@code Comparator} to be used to compare stream elements |
|
17167 | 386 |
* @return the new stream |
387 |
*/ |
|
388 |
Stream<T> sorted(Comparator<? super T> comparator); |
|
389 |
||
390 |
/** |
|
391 |
* Returns a stream consisting of the elements of this stream, additionally |
|
392 |
* performing the provided action on each element as elements are consumed |
|
393 |
* from the resulting stream. |
|
394 |
* |
|
395 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
396 |
* operation</a>. |
|
397 |
* |
|
398 |
* <p>For parallel stream pipelines, the action may be called at |
|
399 |
* whatever time and in whatever thread the element is made available by the |
|
400 |
* upstream operation. If the action modifies shared state, |
|
401 |
* it is responsible for providing the required synchronization. |
|
402 |
* |
|
403 |
* @apiNote This method exists mainly to support debugging, where you want |
|
404 |
* to see the elements as they flow past a certain point in a pipeline: |
|
405 |
* <pre>{@code |
|
21846
c10feb34bc0b
8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents:
21339
diff
changeset
|
406 |
* Stream.of("one", "two", "three", "four") |
c10feb34bc0b
8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents:
21339
diff
changeset
|
407 |
* .filter(e -> e.length() > 3) |
c10feb34bc0b
8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents:
21339
diff
changeset
|
408 |
* .peek(e -> System.out.println("Filtered value: " + e)) |
c10feb34bc0b
8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents:
21339
diff
changeset
|
409 |
* .map(String::toUpperCase) |
c10feb34bc0b
8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents:
21339
diff
changeset
|
410 |
* .peek(e -> System.out.println("Mapped value: " + e)) |
c10feb34bc0b
8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents:
21339
diff
changeset
|
411 |
* .collect(Collectors.toList()); |
17167 | 412 |
* }</pre> |
413 |
* |
|
19850 | 414 |
* @param action a <a href="package-summary.html#NonInterference"> |
17167 | 415 |
* non-interfering</a> action to perform on the elements as |
416 |
* they are consumed from the stream |
|
417 |
* @return the new stream |
|
418 |
*/ |
|
19850 | 419 |
Stream<T> peek(Consumer<? super T> action); |
17167 | 420 |
|
421 |
/** |
|
422 |
* Returns a stream consisting of the elements of this stream, truncated |
|
423 |
* to be no longer than {@code maxSize} in length. |
|
424 |
* |
|
425 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
426 |
* stateful intermediate operation</a>. |
|
427 |
* |
|
20866
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
428 |
* @apiNote |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
429 |
* While {@code limit()} is generally a cheap operation on sequential |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
430 |
* stream pipelines, it can be quite expensive on ordered parallel pipelines, |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
431 |
* especially for large values of {@code maxSize}, since {@code limit(n)} |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
432 |
* is constrained to return not just any <em>n</em> elements, but the |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
433 |
* <em>first n</em> elements in the encounter order. Using an unordered |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
434 |
* stream source (such as {@link #generate(Supplier)}) or removing the |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
435 |
* ordering constraint with {@link #unordered()} may result in significant |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
436 |
* speedups of {@code limit()} in parallel pipelines, if the semantics of |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
437 |
* your situation permit. If consistency with encounter order is required, |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
438 |
* and you are experiencing poor performance or memory utilization with |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
439 |
* {@code limit()} in parallel pipelines, switching to sequential execution |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
440 |
* with {@link #sequential()} may improve performance. |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
441 |
* |
17167 | 442 |
* @param maxSize the number of elements the stream should be limited to |
443 |
* @return the new stream |
|
444 |
* @throws IllegalArgumentException if {@code maxSize} is negative |
|
445 |
*/ |
|
446 |
Stream<T> limit(long maxSize); |
|
447 |
||
448 |
/** |
|
449 |
* Returns a stream consisting of the remaining elements of this stream |
|
20866
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
450 |
* after discarding the first {@code n} elements of the stream. |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
451 |
* If this stream contains fewer than {@code n} elements then an |
17167 | 452 |
* empty stream will be returned. |
453 |
* |
|
454 |
* <p>This is a <a href="package-summary.html#StreamOps">stateful |
|
455 |
* intermediate operation</a>. |
|
456 |
* |
|
20866
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
457 |
* @apiNote |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
458 |
* While {@code skip()} is generally a cheap operation on sequential |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
459 |
* stream pipelines, it can be quite expensive on ordered parallel pipelines, |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
460 |
* especially for large values of {@code n}, since {@code skip(n)} |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
461 |
* is constrained to skip not just any <em>n</em> elements, but the |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
462 |
* <em>first n</em> elements in the encounter order. Using an unordered |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
463 |
* stream source (such as {@link #generate(Supplier)}) or removing the |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
464 |
* ordering constraint with {@link #unordered()} may result in significant |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
465 |
* speedups of {@code skip()} in parallel pipelines, if the semantics of |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
466 |
* your situation permit. If consistency with encounter order is required, |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
467 |
* and you are experiencing poor performance or memory utilization with |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
468 |
* {@code skip()} in parallel pipelines, switching to sequential execution |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
469 |
* with {@link #sequential()} may improve performance. |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
470 |
* |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
471 |
* @param n the number of leading elements to skip |
17167 | 472 |
* @return the new stream |
20866
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
473 |
* @throws IllegalArgumentException if {@code n} is negative |
17167 | 474 |
*/ |
20866
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
475 |
Stream<T> skip(long n); |
17167 | 476 |
|
477 |
/** |
|
478 |
* Performs an action for each element of this stream. |
|
479 |
* |
|
480 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
481 |
* operation</a>. |
|
482 |
* |
|
21339 | 483 |
* <p>The behavior of this operation is explicitly nondeterministic. |
484 |
* For parallel stream pipelines, this operation does <em>not</em> |
|
17167 | 485 |
* guarantee to respect the encounter order of the stream, as doing so |
486 |
* would sacrifice the benefit of parallelism. For any given element, the |
|
487 |
* action may be performed at whatever time and in whatever thread the |
|
488 |
* library chooses. If the action accesses shared state, it is |
|
489 |
* responsible for providing the required synchronization. |
|
490 |
* |
|
491 |
* @param action a <a href="package-summary.html#NonInterference"> |
|
492 |
* non-interfering</a> action to perform on the elements |
|
493 |
*/ |
|
494 |
void forEach(Consumer<? super T> action); |
|
495 |
||
496 |
/** |
|
21339 | 497 |
* Performs an action for each element of this stream, in the encounter |
498 |
* order of the stream if the stream has a defined encounter order. |
|
17167 | 499 |
* |
500 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
501 |
* operation</a>. |
|
502 |
* |
|
21339 | 503 |
* <p>This operation processes the elements one at a time, in encounter |
504 |
* order if one exists. Performing the action for one element |
|
505 |
* <a href="../concurrent/package-summary.html#MemoryVisibility"><i>happens-before</i></a> |
|
506 |
* performing the action for subsequent elements, but for any given element, |
|
507 |
* the action may be performed in whatever thread the library chooses. |
|
508 |
* |
|
17167 | 509 |
* @param action a <a href="package-summary.html#NonInterference"> |
510 |
* non-interfering</a> action to perform on the elements |
|
511 |
* @see #forEach(Consumer) |
|
512 |
*/ |
|
513 |
void forEachOrdered(Consumer<? super T> action); |
|
514 |
||
515 |
/** |
|
516 |
* Returns an array containing the elements of this stream. |
|
517 |
* |
|
518 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
519 |
* operation</a>. |
|
520 |
* |
|
521 |
* @return an array containing the elements of this stream |
|
522 |
*/ |
|
523 |
Object[] toArray(); |
|
524 |
||
525 |
/** |
|
526 |
* Returns an array containing the elements of this stream, using the |
|
19850 | 527 |
* provided {@code generator} function to allocate the returned array, as |
528 |
* well as any additional arrays that might be required for a partitioned |
|
529 |
* execution or for resizing. |
|
17167 | 530 |
* |
531 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
532 |
* operation</a>. |
|
533 |
* |
|
19850 | 534 |
* @apiNote |
535 |
* The generator function takes an integer, which is the size of the |
|
536 |
* desired array, and produces an array of the desired size. This can be |
|
537 |
* concisely expressed with an array constructor reference: |
|
538 |
* <pre>{@code |
|
539 |
* Person[] men = people.stream() |
|
540 |
* .filter(p -> p.getGender() == MALE) |
|
541 |
* .toArray(Person[]::new); |
|
542 |
* }</pre> |
|
543 |
* |
|
17167 | 544 |
* @param <A> the element type of the resulting array |
545 |
* @param generator a function which produces a new array of the desired |
|
546 |
* type and the provided length |
|
547 |
* @return an array containing the elements in this stream |
|
548 |
* @throws ArrayStoreException if the runtime type of the array returned |
|
549 |
* from the array generator is not a supertype of the runtime type of every |
|
550 |
* element in this stream |
|
551 |
*/ |
|
552 |
<A> A[] toArray(IntFunction<A[]> generator); |
|
553 |
||
554 |
/** |
|
555 |
* Performs a <a href="package-summary.html#Reduction">reduction</a> on the |
|
556 |
* elements of this stream, using the provided identity value and an |
|
557 |
* <a href="package-summary.html#Associativity">associative</a> |
|
558 |
* accumulation function, and returns the reduced value. This is equivalent |
|
559 |
* to: |
|
560 |
* <pre>{@code |
|
561 |
* T result = identity; |
|
562 |
* for (T element : this stream) |
|
563 |
* result = accumulator.apply(result, element) |
|
564 |
* return result; |
|
565 |
* }</pre> |
|
566 |
* |
|
567 |
* but is not constrained to execute sequentially. |
|
568 |
* |
|
569 |
* <p>The {@code identity} value must be an identity for the accumulator |
|
570 |
* function. This means that for all {@code t}, |
|
571 |
* {@code accumulator.apply(identity, t)} is equal to {@code t}. |
|
572 |
* The {@code accumulator} function must be an |
|
573 |
* <a href="package-summary.html#Associativity">associative</a> function. |
|
574 |
* |
|
575 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
576 |
* operation</a>. |
|
577 |
* |
|
578 |
* @apiNote Sum, min, max, average, and string concatenation are all special |
|
579 |
* cases of reduction. Summing a stream of numbers can be expressed as: |
|
580 |
* |
|
581 |
* <pre>{@code |
|
582 |
* Integer sum = integers.reduce(0, (a, b) -> a+b); |
|
583 |
* }</pre> |
|
584 |
* |
|
19850 | 585 |
* or: |
17167 | 586 |
* |
587 |
* <pre>{@code |
|
588 |
* Integer sum = integers.reduce(0, Integer::sum); |
|
589 |
* }</pre> |
|
590 |
* |
|
591 |
* <p>While this may seem a more roundabout way to perform an aggregation |
|
592 |
* compared to simply mutating a running total in a loop, reduction |
|
593 |
* operations parallelize more gracefully, without needing additional |
|
594 |
* synchronization and with greatly reduced risk of data races. |
|
595 |
* |
|
596 |
* @param identity the identity value for the accumulating function |
|
21339 | 597 |
* @param accumulator an <a href="package-summary.html#Associativity">associative</a>, |
598 |
* <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
599 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
600 |
* function for combining two values |
|
17167 | 601 |
* @return the result of the reduction |
602 |
*/ |
|
603 |
T reduce(T identity, BinaryOperator<T> accumulator); |
|
604 |
||
605 |
/** |
|
606 |
* Performs a <a href="package-summary.html#Reduction">reduction</a> on the |
|
607 |
* elements of this stream, using an |
|
608 |
* <a href="package-summary.html#Associativity">associative</a> accumulation |
|
609 |
* function, and returns an {@code Optional} describing the reduced value, |
|
610 |
* if any. This is equivalent to: |
|
611 |
* <pre>{@code |
|
612 |
* boolean foundAny = false; |
|
613 |
* T result = null; |
|
614 |
* for (T element : this stream) { |
|
615 |
* if (!foundAny) { |
|
616 |
* foundAny = true; |
|
617 |
* result = element; |
|
618 |
* } |
|
619 |
* else |
|
620 |
* result = accumulator.apply(result, element); |
|
621 |
* } |
|
622 |
* return foundAny ? Optional.of(result) : Optional.empty(); |
|
623 |
* }</pre> |
|
624 |
* |
|
625 |
* but is not constrained to execute sequentially. |
|
626 |
* |
|
627 |
* <p>The {@code accumulator} function must be an |
|
628 |
* <a href="package-summary.html#Associativity">associative</a> function. |
|
629 |
* |
|
630 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
631 |
* operation</a>. |
|
632 |
* |
|
21339 | 633 |
* @param accumulator an <a href="package-summary.html#Associativity">associative</a>, |
634 |
* <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
635 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
636 |
* function for combining two values |
|
19850 | 637 |
* @return an {@link Optional} describing the result of the reduction |
638 |
* @throws NullPointerException if the result of the reduction is null |
|
17167 | 639 |
* @see #reduce(Object, BinaryOperator) |
21339 | 640 |
* @see #min(Comparator) |
641 |
* @see #max(Comparator) |
|
17167 | 642 |
*/ |
643 |
Optional<T> reduce(BinaryOperator<T> accumulator); |
|
644 |
||
645 |
/** |
|
646 |
* Performs a <a href="package-summary.html#Reduction">reduction</a> on the |
|
19859
ac48498acd3a
8024825: Some fixes are missing from java.util.stream spec update
henryjen
parents:
19850
diff
changeset
|
647 |
* elements of this stream, using the provided identity, accumulation and |
ac48498acd3a
8024825: Some fixes are missing from java.util.stream spec update
henryjen
parents:
19850
diff
changeset
|
648 |
* combining functions. This is equivalent to: |
17167 | 649 |
* <pre>{@code |
650 |
* U result = identity; |
|
651 |
* for (T element : this stream) |
|
652 |
* result = accumulator.apply(result, element) |
|
653 |
* return result; |
|
654 |
* }</pre> |
|
655 |
* |
|
656 |
* but is not constrained to execute sequentially. |
|
657 |
* |
|
658 |
* <p>The {@code identity} value must be an identity for the combiner |
|
659 |
* function. This means that for all {@code u}, {@code combiner(identity, u)} |
|
660 |
* is equal to {@code u}. Additionally, the {@code combiner} function |
|
661 |
* must be compatible with the {@code accumulator} function; for all |
|
662 |
* {@code u} and {@code t}, the following must hold: |
|
663 |
* <pre>{@code |
|
664 |
* combiner.apply(u, accumulator.apply(identity, t)) == accumulator.apply(u, t) |
|
665 |
* }</pre> |
|
666 |
* |
|
667 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
668 |
* operation</a>. |
|
669 |
* |
|
670 |
* @apiNote Many reductions using this form can be represented more simply |
|
671 |
* by an explicit combination of {@code map} and {@code reduce} operations. |
|
672 |
* The {@code accumulator} function acts as a fused mapper and accumulator, |
|
673 |
* which can sometimes be more efficient than separate mapping and reduction, |
|
19850 | 674 |
* such as when knowing the previously reduced value allows you to avoid |
675 |
* some computation. |
|
17167 | 676 |
* |
677 |
* @param <U> The type of the result |
|
678 |
* @param identity the identity value for the combiner function |
|
21339 | 679 |
* @param accumulator an <a href="package-summary.html#Associativity">associative</a>, |
680 |
* <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
681 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
682 |
* function for incorporating an additional element into a result |
|
683 |
* @param combiner an <a href="package-summary.html#Associativity">associative</a>, |
|
684 |
* <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
685 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
686 |
* function for combining two values, which must be |
|
687 |
* compatible with the accumulator function |
|
17167 | 688 |
* @return the result of the reduction |
689 |
* @see #reduce(BinaryOperator) |
|
690 |
* @see #reduce(Object, BinaryOperator) |
|
691 |
*/ |
|
692 |
<U> U reduce(U identity, |
|
693 |
BiFunction<U, ? super T, U> accumulator, |
|
694 |
BinaryOperator<U> combiner); |
|
695 |
||
696 |
/** |
|
697 |
* Performs a <a href="package-summary.html#MutableReduction">mutable |
|
698 |
* reduction</a> operation on the elements of this stream. A mutable |
|
19850 | 699 |
* reduction is one in which the reduced value is a mutable result container, |
17167 | 700 |
* such as an {@code ArrayList}, and elements are incorporated by updating |
19850 | 701 |
* the state of the result rather than by replacing the result. This |
17167 | 702 |
* produces a result equivalent to: |
703 |
* <pre>{@code |
|
19850 | 704 |
* R result = supplier.get(); |
17167 | 705 |
* for (T element : this stream) |
706 |
* accumulator.accept(result, element); |
|
707 |
* return result; |
|
708 |
* }</pre> |
|
709 |
* |
|
710 |
* <p>Like {@link #reduce(Object, BinaryOperator)}, {@code collect} operations |
|
711 |
* can be parallelized without requiring additional synchronization. |
|
712 |
* |
|
713 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
714 |
* operation</a>. |
|
715 |
* |
|
716 |
* @apiNote There are many existing classes in the JDK whose signatures are |
|
19850 | 717 |
* well-suited for use with method references as arguments to {@code collect()}. |
718 |
* For example, the following will accumulate strings into an {@code ArrayList}: |
|
17167 | 719 |
* <pre>{@code |
19850 | 720 |
* List<String> asList = stringStream.collect(ArrayList::new, ArrayList::add, |
721 |
* ArrayList::addAll); |
|
17167 | 722 |
* }</pre> |
723 |
* |
|
724 |
* <p>The following will take a stream of strings and concatenates them into a |
|
725 |
* single string: |
|
726 |
* <pre>{@code |
|
727 |
* String concat = stringStream.collect(StringBuilder::new, StringBuilder::append, |
|
728 |
* StringBuilder::append) |
|
729 |
* .toString(); |
|
730 |
* }</pre> |
|
731 |
* |
|
732 |
* @param <R> type of the result |
|
19850 | 733 |
* @param supplier a function that creates a new result container. For a |
734 |
* parallel execution, this function may be called |
|
735 |
* multiple times and must return a fresh value each time. |
|
21339 | 736 |
* @param accumulator an <a href="package-summary.html#Associativity">associative</a>, |
737 |
* <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
738 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
739 |
* function for incorporating an additional element into a result |
|
740 |
* @param combiner an <a href="package-summary.html#Associativity">associative</a>, |
|
741 |
* <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
742 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
743 |
* function for combining two values, which must be |
|
744 |
* compatible with the accumulator function |
|
17167 | 745 |
* @return the result of the reduction |
746 |
*/ |
|
19850 | 747 |
<R> R collect(Supplier<R> supplier, |
17167 | 748 |
BiConsumer<R, ? super T> accumulator, |
749 |
BiConsumer<R, R> combiner); |
|
750 |
||
751 |
/** |
|
752 |
* Performs a <a href="package-summary.html#MutableReduction">mutable |
|
753 |
* reduction</a> operation on the elements of this stream using a |
|
19850 | 754 |
* {@code Collector}. A {@code Collector} |
17167 | 755 |
* encapsulates the functions used as arguments to |
756 |
* {@link #collect(Supplier, BiConsumer, BiConsumer)}, allowing for reuse of |
|
19850 | 757 |
* collection strategies and composition of collect operations such as |
17167 | 758 |
* multiple-level grouping or partitioning. |
759 |
* |
|
21339 | 760 |
* <p>If the stream is parallel, and the {@code Collector} |
761 |
* is {@link Collector.Characteristics#CONCURRENT concurrent}, and |
|
762 |
* either the stream is unordered or the collector is |
|
763 |
* {@link Collector.Characteristics#UNORDERED unordered}, |
|
764 |
* then a concurrent reduction will be performed (see {@link Collector} for |
|
765 |
* details on concurrent reduction.) |
|
766 |
* |
|
17167 | 767 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
768 |
* operation</a>. |
|
769 |
* |
|
770 |
* <p>When executed in parallel, multiple intermediate results may be |
|
19850 | 771 |
* instantiated, populated, and merged so as to maintain isolation of |
17167 | 772 |
* mutable data structures. Therefore, even when executed in parallel |
773 |
* with non-thread-safe data structures (such as {@code ArrayList}), no |
|
774 |
* additional synchronization is needed for a parallel reduction. |
|
775 |
* |
|
776 |
* @apiNote |
|
777 |
* The following will accumulate strings into an ArrayList: |
|
778 |
* <pre>{@code |
|
779 |
* List<String> asList = stringStream.collect(Collectors.toList()); |
|
780 |
* }</pre> |
|
781 |
* |
|
782 |
* <p>The following will classify {@code Person} objects by city: |
|
783 |
* <pre>{@code |
|
19850 | 784 |
* Map<String, List<Person>> peopleByCity |
785 |
* = personStream.collect(Collectors.groupingBy(Person::getCity)); |
|
17167 | 786 |
* }</pre> |
787 |
* |
|
788 |
* <p>The following will classify {@code Person} objects by state and city, |
|
789 |
* cascading two {@code Collector}s together: |
|
790 |
* <pre>{@code |
|
19850 | 791 |
* Map<String, Map<String, List<Person>>> peopleByStateAndCity |
792 |
* = personStream.collect(Collectors.groupingBy(Person::getState, |
|
793 |
* Collectors.groupingBy(Person::getCity))); |
|
17167 | 794 |
* }</pre> |
795 |
* |
|
796 |
* @param <R> the type of the result |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
19199
diff
changeset
|
797 |
* @param <A> the intermediate accumulation type of the {@code Collector} |
17167 | 798 |
* @param collector the {@code Collector} describing the reduction |
799 |
* @return the result of the reduction |
|
800 |
* @see #collect(Supplier, BiConsumer, BiConsumer) |
|
801 |
* @see Collectors |
|
802 |
*/ |
|
19799
efa9ff09b024
8024178: Difference in Stream.collect(Collector) methods located in jdk8 and jdk8-lambda repos
henryjen
parents:
19214
diff
changeset
|
803 |
<R, A> R collect(Collector<? super T, A, R> collector); |
17167 | 804 |
|
805 |
/** |
|
806 |
* Returns the minimum element of this stream according to the provided |
|
807 |
* {@code Comparator}. This is a special case of a |
|
19850 | 808 |
* <a href="package-summary.html#Reduction">reduction</a>. |
17167 | 809 |
* |
810 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>. |
|
811 |
* |
|
21339 | 812 |
* @param comparator a <a href="package-summary.html#NonInterference">non-interfering</a>, |
813 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
814 |
* {@code Comparator} to compare elements of this stream |
|
17167 | 815 |
* @return an {@code Optional} describing the minimum element of this stream, |
816 |
* or an empty {@code Optional} if the stream is empty |
|
19850 | 817 |
* @throws NullPointerException if the minimum element is null |
17167 | 818 |
*/ |
819 |
Optional<T> min(Comparator<? super T> comparator); |
|
820 |
||
821 |
/** |
|
822 |
* Returns the maximum element of this stream according to the provided |
|
823 |
* {@code Comparator}. This is a special case of a |
|
19850 | 824 |
* <a href="package-summary.html#Reduction">reduction</a>. |
17167 | 825 |
* |
826 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
827 |
* operation</a>. |
|
828 |
* |
|
21339 | 829 |
* @param comparator a <a href="package-summary.html#NonInterference">non-interfering</a>, |
830 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
831 |
* {@code Comparator} to compare elements of this stream |
|
17167 | 832 |
* @return an {@code Optional} describing the maximum element of this stream, |
833 |
* or an empty {@code Optional} if the stream is empty |
|
19850 | 834 |
* @throws NullPointerException if the maximum element is null |
17167 | 835 |
*/ |
836 |
Optional<T> max(Comparator<? super T> comparator); |
|
837 |
||
838 |
/** |
|
839 |
* Returns the count of elements in this stream. This is a special case of |
|
19850 | 840 |
* a <a href="package-summary.html#Reduction">reduction</a> and is |
17167 | 841 |
* equivalent to: |
842 |
* <pre>{@code |
|
843 |
* return mapToLong(e -> 1L).sum(); |
|
844 |
* }</pre> |
|
845 |
* |
|
846 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>. |
|
847 |
* |
|
848 |
* @return the count of elements in this stream |
|
849 |
*/ |
|
850 |
long count(); |
|
851 |
||
852 |
/** |
|
853 |
* Returns whether any elements of this stream match the provided |
|
854 |
* predicate. May not evaluate the predicate on all elements if not |
|
21339 | 855 |
* necessary for determining the result. If the stream is empty then |
856 |
* {@code false} is returned and the predicate is not evaluated. |
|
17167 | 857 |
* |
858 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
859 |
* terminal operation</a>. |
|
860 |
* |
|
21339 | 861 |
* @apiNote |
862 |
* This method evaluates the <em>existential quantification</em> of the |
|
863 |
* predicate over the elements of the stream (for some x P(x)). |
|
864 |
* |
|
865 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
866 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
867 |
* predicate to apply to elements of this stream |
|
17167 | 868 |
* @return {@code true} if any elements of the stream match the provided |
21339 | 869 |
* predicate, otherwise {@code false} |
17167 | 870 |
*/ |
871 |
boolean anyMatch(Predicate<? super T> predicate); |
|
872 |
||
873 |
/** |
|
874 |
* Returns whether all elements of this stream match the provided predicate. |
|
875 |
* May not evaluate the predicate on all elements if not necessary for |
|
21339 | 876 |
* determining the result. If the stream is empty then {@code true} is |
877 |
* returned and the predicate is not evaluated. |
|
17167 | 878 |
* |
879 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
880 |
* terminal operation</a>. |
|
881 |
* |
|
21339 | 882 |
* @apiNote |
883 |
* This method evaluates the <em>universal quantification</em> of the |
|
884 |
* predicate over the elements of the stream (for all x P(x)). If the |
|
885 |
* stream is empty, the quantification is said to be <em>vacuously |
|
886 |
* satisfied</em> and is always {@code true} (regardless of P(x)). |
|
887 |
* |
|
888 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
889 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
890 |
* predicate to apply to elements of this stream |
|
891 |
* @return {@code true} if either all elements of the stream match the |
|
892 |
* provided predicate or the stream is empty, otherwise {@code false} |
|
17167 | 893 |
*/ |
894 |
boolean allMatch(Predicate<? super T> predicate); |
|
895 |
||
896 |
/** |
|
897 |
* Returns whether no elements of this stream match the provided predicate. |
|
898 |
* May not evaluate the predicate on all elements if not necessary for |
|
21339 | 899 |
* determining the result. If the stream is empty then {@code true} is |
900 |
* returned and the predicate is not evaluated. |
|
17167 | 901 |
* |
902 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
903 |
* terminal operation</a>. |
|
904 |
* |
|
21339 | 905 |
* @apiNote |
906 |
* This method evaluates the <em>universal quantification</em> of the |
|
907 |
* negated predicate over the elements of the stream (for all x ~P(x)). If |
|
908 |
* the stream is empty, the quantification is said to be vacuously satisfied |
|
909 |
* and is always {@code true}, regardless of P(x). |
|
910 |
* |
|
911 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
912 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
913 |
* predicate to apply to elements of this stream |
|
914 |
* @return {@code true} if either no elements of the stream match the |
|
915 |
* provided predicate or the stream is empty, otherwise {@code false} |
|
17167 | 916 |
*/ |
917 |
boolean noneMatch(Predicate<? super T> predicate); |
|
918 |
||
919 |
/** |
|
19850 | 920 |
* Returns an {@link Optional} describing the first element of this stream, |
921 |
* or an empty {@code Optional} if the stream is empty. If the stream has |
|
922 |
* no encounter order, then any element may be returned. |
|
17167 | 923 |
* |
924 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
925 |
* terminal operation</a>. |
|
926 |
* |
|
927 |
* @return an {@code Optional} describing the first element of this stream, |
|
928 |
* or an empty {@code Optional} if the stream is empty |
|
929 |
* @throws NullPointerException if the element selected is null |
|
930 |
*/ |
|
931 |
Optional<T> findFirst(); |
|
932 |
||
933 |
/** |
|
934 |
* Returns an {@link Optional} describing some element of the stream, or an |
|
935 |
* empty {@code Optional} if the stream is empty. |
|
936 |
* |
|
937 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
938 |
* terminal operation</a>. |
|
939 |
* |
|
940 |
* <p>The behavior of this operation is explicitly nondeterministic; it is |
|
941 |
* free to select any element in the stream. This is to allow for maximal |
|
942 |
* performance in parallel operations; the cost is that multiple invocations |
|
19850 | 943 |
* on the same source may not return the same result. (If a stable result |
944 |
* is desired, use {@link #findFirst()} instead.) |
|
17167 | 945 |
* |
946 |
* @return an {@code Optional} describing some element of this stream, or an |
|
947 |
* empty {@code Optional} if the stream is empty |
|
948 |
* @throws NullPointerException if the element selected is null |
|
949 |
* @see #findFirst() |
|
950 |
*/ |
|
951 |
Optional<T> findAny(); |
|
17195 | 952 |
|
953 |
// Static factories |
|
954 |
||
955 |
/** |
|
956 |
* Returns a builder for a {@code Stream}. |
|
957 |
* |
|
958 |
* @param <T> type of elements |
|
959 |
* @return a stream builder |
|
960 |
*/ |
|
18825
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
961 |
public static<T> Builder<T> builder() { |
17195 | 962 |
return new Streams.StreamBuilderImpl<>(); |
963 |
} |
|
964 |
||
965 |
/** |
|
966 |
* Returns an empty sequential {@code Stream}. |
|
967 |
* |
|
968 |
* @param <T> the type of stream elements |
|
969 |
* @return an empty sequential stream |
|
970 |
*/ |
|
971 |
public static<T> Stream<T> empty() { |
|
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
972 |
return StreamSupport.stream(Spliterators.<T>emptySpliterator(), false); |
17195 | 973 |
} |
974 |
||
975 |
/** |
|
976 |
* Returns a sequential {@code Stream} containing a single element. |
|
977 |
* |
|
978 |
* @param t the single element |
|
979 |
* @param <T> the type of stream elements |
|
980 |
* @return a singleton sequential stream |
|
981 |
*/ |
|
982 |
public static<T> Stream<T> of(T t) { |
|
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
983 |
return StreamSupport.stream(new Streams.StreamBuilderImpl<>(t), false); |
17195 | 984 |
} |
985 |
||
986 |
/** |
|
19850 | 987 |
* Returns a sequential ordered stream whose elements are the specified values. |
17195 | 988 |
* |
989 |
* @param <T> the type of stream elements |
|
990 |
* @param values the elements of the new stream |
|
991 |
* @return the new stream |
|
992 |
*/ |
|
993 |
@SafeVarargs |
|
19859
ac48498acd3a
8024825: Some fixes are missing from java.util.stream spec update
henryjen
parents:
19850
diff
changeset
|
994 |
@SuppressWarnings("varargs") // Creating a stream from an array is safe |
17195 | 995 |
public static<T> Stream<T> of(T... values) { |
996 |
return Arrays.stream(values); |
|
997 |
} |
|
998 |
||
999 |
/** |
|
19850 | 1000 |
* Returns an infinite sequential ordered {@code Stream} produced by iterative |
17195 | 1001 |
* application of a function {@code f} to an initial element {@code seed}, |
1002 |
* producing a {@code Stream} consisting of {@code seed}, {@code f(seed)}, |
|
1003 |
* {@code f(f(seed))}, etc. |
|
1004 |
* |
|
1005 |
* <p>The first element (position {@code 0}) in the {@code Stream} will be |
|
1006 |
* the provided {@code seed}. For {@code n > 0}, the element at position |
|
1007 |
* {@code n}, will be the result of applying the function {@code f} to the |
|
1008 |
* element at position {@code n - 1}. |
|
1009 |
* |
|
1010 |
* @param <T> the type of stream elements |
|
1011 |
* @param seed the initial element |
|
1012 |
* @param f a function to be applied to to the previous element to produce |
|
1013 |
* a new element |
|
1014 |
* @return a new sequential {@code Stream} |
|
1015 |
*/ |
|
1016 |
public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f) { |
|
1017 |
Objects.requireNonNull(f); |
|
1018 |
final Iterator<T> iterator = new Iterator<T>() { |
|
1019 |
@SuppressWarnings("unchecked") |
|
1020 |
T t = (T) Streams.NONE; |
|
1021 |
||
1022 |
@Override |
|
1023 |
public boolean hasNext() { |
|
1024 |
return true; |
|
1025 |
} |
|
1026 |
||
1027 |
@Override |
|
1028 |
public T next() { |
|
1029 |
return t = (t == Streams.NONE) ? seed : f.apply(t); |
|
1030 |
} |
|
1031 |
}; |
|
1032 |
return StreamSupport.stream(Spliterators.spliteratorUnknownSize( |
|
1033 |
iterator, |
|
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
1034 |
Spliterator.ORDERED | Spliterator.IMMUTABLE), false); |
17195 | 1035 |
} |
1036 |
||
1037 |
/** |
|
21339 | 1038 |
* Returns an infinite sequential unordered stream where each element is |
1039 |
* generated by the provided {@code Supplier}. This is suitable for |
|
1040 |
* generating constant streams, streams of random elements, etc. |
|
17195 | 1041 |
* |
1042 |
* @param <T> the type of stream elements |
|
1043 |
* @param s the {@code Supplier} of generated elements |
|
21339 | 1044 |
* @return a new infinite sequential unordered {@code Stream} |
17195 | 1045 |
*/ |
1046 |
public static<T> Stream<T> generate(Supplier<T> s) { |
|
1047 |
Objects.requireNonNull(s); |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
17914
diff
changeset
|
1048 |
return StreamSupport.stream( |
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
1049 |
new StreamSpliterators.InfiniteSupplyingSpliterator.OfRef<>(Long.MAX_VALUE, s), false); |
17195 | 1050 |
} |
18820 | 1051 |
|
1052 |
/** |
|
19850 | 1053 |
* Creates a lazily concatenated stream whose elements are all the |
1054 |
* elements of the first stream followed by all the elements of the |
|
21339 | 1055 |
* second stream. The resulting stream is ordered if both |
18820 | 1056 |
* of the input streams are ordered, and parallel if either of the input |
19800 | 1057 |
* streams is parallel. When the resulting stream is closed, the close |
1058 |
* handlers for both input streams are invoked. |
|
18820 | 1059 |
* |
21339 | 1060 |
* @implNote |
1061 |
* Use caution when constructing streams from repeated concatenation. |
|
1062 |
* Accessing an element of a deeply concatenated stream can result in deep |
|
1063 |
* call chains, or even {@code StackOverflowException}. |
|
1064 |
* |
|
18820 | 1065 |
* @param <T> The type of stream elements |
1066 |
* @param a the first stream |
|
19850 | 1067 |
* @param b the second stream |
18820 | 1068 |
* @return the concatenation of the two input streams |
1069 |
*/ |
|
1070 |
public static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b) { |
|
1071 |
Objects.requireNonNull(a); |
|
1072 |
Objects.requireNonNull(b); |
|
1073 |
||
1074 |
@SuppressWarnings("unchecked") |
|
1075 |
Spliterator<T> split = new Streams.ConcatSpliterator.OfRef<>( |
|
1076 |
(Spliterator<T>) a.spliterator(), (Spliterator<T>) b.spliterator()); |
|
19800 | 1077 |
Stream<T> stream = StreamSupport.stream(split, a.isParallel() || b.isParallel()); |
1078 |
return stream.onClose(Streams.composedClose(a, b)); |
|
18820 | 1079 |
} |
18825
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1080 |
|
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1081 |
/** |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1082 |
* A mutable builder for a {@code Stream}. This allows the creation of a |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1083 |
* {@code Stream} by generating elements individually and adding them to the |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1084 |
* {@code Builder} (without the copying overhead that comes from using |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1085 |
* an {@code ArrayList} as a temporary buffer.) |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1086 |
* |
19850 | 1087 |
* <p>A stream builder has a lifecycle, which starts in a building |
18825
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1088 |
* phase, during which elements can be added, and then transitions to a built |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1089 |
* phase, after which elements may not be added. The built phase begins |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1090 |
* when the {@link #build()} method is called, which creates an ordered |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1091 |
* {@code Stream} whose elements are the elements that were added to the stream |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1092 |
* builder, in the order they were added. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1093 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1094 |
* @param <T> the type of stream elements |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1095 |
* @see Stream#builder() |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1096 |
* @since 1.8 |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1097 |
*/ |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1098 |
public interface Builder<T> extends Consumer<T> { |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1099 |
|
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1100 |
/** |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1101 |
* Adds an element to the stream being built. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1102 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1103 |
* @throws IllegalStateException if the builder has already transitioned to |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1104 |
* the built state |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1105 |
*/ |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1106 |
@Override |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1107 |
void accept(T t); |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1108 |
|
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1109 |
/** |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1110 |
* Adds an element to the stream being built. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1111 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1112 |
* @implSpec |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1113 |
* The default implementation behaves as if: |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1114 |
* <pre>{@code |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1115 |
* accept(t) |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1116 |
* return this; |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1117 |
* }</pre> |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1118 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1119 |
* @param t the element to add |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1120 |
* @return {@code this} builder |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1121 |
* @throws IllegalStateException if the builder has already transitioned to |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1122 |
* the built state |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1123 |
*/ |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1124 |
default Builder<T> add(T t) { |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1125 |
accept(t); |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1126 |
return this; |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1127 |
} |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1128 |
|
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1129 |
/** |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1130 |
* Builds the stream, transitioning this builder to the built state. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1131 |
* An {@code IllegalStateException} is thrown if there are further attempts |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1132 |
* to operate on the builder after it has entered the built state. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1133 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1134 |
* @return the built stream |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1135 |
* @throws IllegalStateException if the builder has already transitioned to |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1136 |
* the built state |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1137 |
*/ |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1138 |
Stream<T> build(); |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1139 |
|
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1140 |
} |
17167 | 1141 |
} |