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 |
||
17195 | 27 |
import java.util.Arrays; |
17167 | 28 |
import java.util.IntSummaryStatistics; |
17195 | 29 |
import java.util.Objects; |
17167 | 30 |
import java.util.OptionalDouble; |
31 |
import java.util.OptionalInt; |
|
32 |
import java.util.PrimitiveIterator; |
|
33 |
import java.util.Spliterator; |
|
17195 | 34 |
import java.util.Spliterators; |
17167 | 35 |
import java.util.function.BiConsumer; |
36 |
import java.util.function.Function; |
|
37 |
import java.util.function.IntBinaryOperator; |
|
38 |
import java.util.function.IntConsumer; |
|
39 |
import java.util.function.IntFunction; |
|
40 |
import java.util.function.IntPredicate; |
|
17195 | 41 |
import java.util.function.IntSupplier; |
17167 | 42 |
import java.util.function.IntToDoubleFunction; |
43 |
import java.util.function.IntToLongFunction; |
|
44 |
import java.util.function.IntUnaryOperator; |
|
45 |
import java.util.function.ObjIntConsumer; |
|
46 |
import java.util.function.Supplier; |
|
47 |
||
48 |
/** |
|
21339 | 49 |
* A sequence of primitive int-valued elements supporting sequential and parallel |
50 |
* aggregate operations. This is the {@code int} primitive specialization of |
|
51 |
* {@link Stream}. |
|
52 |
* |
|
53 |
* <p>The following example illustrates an aggregate operation using |
|
54 |
* {@link Stream} and {@link IntStream}, computing the sum of the weights of the |
|
55 |
* red widgets: |
|
19850 | 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 |
* |
|
21339 | 64 |
* See the class documentation for {@link Stream} and the package documentation |
65 |
* for <a href="package-summary.html">java.util.stream</a> for additional |
|
66 |
* specification of streams, stream operations, stream pipelines, and |
|
67 |
* parallelism. |
|
17167 | 68 |
* |
69 |
* @since 1.8 |
|
21339 | 70 |
* @see Stream |
17167 | 71 |
* @see <a href="package-summary.html">java.util.stream</a> |
72 |
*/ |
|
73 |
public interface IntStream extends BaseStream<Integer, IntStream> { |
|
74 |
||
75 |
/** |
|
76 |
* Returns a stream consisting of the elements of this stream that match |
|
77 |
* the given predicate. |
|
78 |
* |
|
79 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
80 |
* operation</a>. |
|
81 |
* |
|
21339 | 82 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>, |
83 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
84 |
* predicate to apply to each element to determine if it |
|
85 |
* should be included |
|
17167 | 86 |
* @return the new stream |
87 |
*/ |
|
88 |
IntStream filter(IntPredicate predicate); |
|
89 |
||
90 |
/** |
|
91 |
* Returns a stream consisting of the results of applying the given |
|
92 |
* function to the elements of this stream. |
|
93 |
* |
|
94 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
95 |
* operation</a>. |
|
96 |
* |
|
21339 | 97 |
* @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>, |
98 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
99 |
* function to apply to each element |
|
17167 | 100 |
* @return the new stream |
101 |
*/ |
|
102 |
IntStream map(IntUnaryOperator mapper); |
|
103 |
||
104 |
/** |
|
105 |
* Returns an object-valued {@code Stream} consisting of the results of |
|
106 |
* applying the given function to the elements of this stream. |
|
107 |
* |
|
108 |
* <p>This is an <a href="package-summary.html#StreamOps"> |
|
109 |
* intermediate operation</a>. |
|
110 |
* |
|
111 |
* @param <U> the element type of the new stream |
|
21339 | 112 |
* @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>, |
113 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
114 |
* function to apply to each element |
|
17167 | 115 |
* @return the new stream |
116 |
*/ |
|
117 |
<U> Stream<U> mapToObj(IntFunction<? extends U> mapper); |
|
118 |
||
119 |
/** |
|
120 |
* Returns a {@code LongStream} consisting of the results of applying the |
|
121 |
* given function to the elements of this stream. |
|
122 |
* |
|
123 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
124 |
* operation</a>. |
|
125 |
* |
|
21339 | 126 |
* @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>, |
127 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
128 |
* function to apply to each element |
|
17167 | 129 |
* @return the new stream |
130 |
*/ |
|
131 |
LongStream mapToLong(IntToLongFunction mapper); |
|
132 |
||
133 |
/** |
|
134 |
* Returns a {@code DoubleStream} consisting of the results of applying the |
|
135 |
* given function to the elements of this stream. |
|
136 |
* |
|
137 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
138 |
* operation</a>. |
|
139 |
* |
|
21339 | 140 |
* @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>, |
141 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
142 |
* function to apply to each element |
|
17167 | 143 |
* @return the new stream |
144 |
*/ |
|
145 |
DoubleStream mapToDouble(IntToDoubleFunction mapper); |
|
146 |
||
147 |
/** |
|
148 |
* Returns a stream consisting of the results of replacing each element of |
|
149 |
* this stream with the contents of the stream produced by applying the |
|
19850 | 150 |
* provided mapping function to each element. (If the result of the mapping |
151 |
* function is {@code null}, this is treated as if the result was an empty |
|
152 |
* stream.) |
|
17167 | 153 |
* |
154 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
155 |
* operation</a>. |
|
156 |
* |
|
21339 | 157 |
* @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>, |
158 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
159 |
* function to apply to each element which produces an |
|
160 |
* {@code IntStream} of new values |
|
17167 | 161 |
* @return the new stream |
162 |
* @see Stream#flatMap(Function) |
|
163 |
*/ |
|
164 |
IntStream flatMap(IntFunction<? extends IntStream> mapper); |
|
165 |
||
166 |
/** |
|
167 |
* Returns a stream consisting of the distinct elements of this stream. |
|
168 |
* |
|
169 |
* <p>This is a <a href="package-summary.html#StreamOps">stateful |
|
170 |
* intermediate operation</a>. |
|
171 |
* |
|
172 |
* @return the new stream |
|
173 |
*/ |
|
174 |
IntStream distinct(); |
|
175 |
||
176 |
/** |
|
177 |
* Returns a stream consisting of the elements of this stream in sorted |
|
178 |
* order. |
|
179 |
* |
|
180 |
* <p>This is a <a href="package-summary.html#StreamOps">stateful |
|
181 |
* intermediate operation</a>. |
|
182 |
* |
|
183 |
* @return the new stream |
|
184 |
*/ |
|
185 |
IntStream sorted(); |
|
186 |
||
187 |
/** |
|
188 |
* Returns a stream consisting of the elements of this stream, additionally |
|
189 |
* performing the provided action on each element as elements are consumed |
|
190 |
* from the resulting stream. |
|
191 |
* |
|
192 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
193 |
* operation</a>. |
|
194 |
* |
|
195 |
* <p>For parallel stream pipelines, the action may be called at |
|
196 |
* whatever time and in whatever thread the element is made available by the |
|
197 |
* upstream operation. If the action modifies shared state, |
|
198 |
* it is responsible for providing the required synchronization. |
|
199 |
* |
|
200 |
* @apiNote This method exists mainly to support debugging, where you want |
|
201 |
* to see the elements as they flow past a certain point in a pipeline: |
|
202 |
* <pre>{@code |
|
21846
c10feb34bc0b
8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents:
21339
diff
changeset
|
203 |
* IntStream.of(1, 2, 3, 4) |
c10feb34bc0b
8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents:
21339
diff
changeset
|
204 |
* .filter(e -> e > 2) |
c10feb34bc0b
8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents:
21339
diff
changeset
|
205 |
* .peek(e -> System.out.println("Filtered value: " + e)) |
c10feb34bc0b
8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents:
21339
diff
changeset
|
206 |
* .map(e -> e * e) |
c10feb34bc0b
8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents:
21339
diff
changeset
|
207 |
* .peek(e -> System.out.println("Mapped value: " + e)) |
c10feb34bc0b
8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents:
21339
diff
changeset
|
208 |
* .sum(); |
17167 | 209 |
* }</pre> |
210 |
* |
|
19850 | 211 |
* @param action a <a href="package-summary.html#NonInterference"> |
212 |
* non-interfering</a> action to perform on the elements as |
|
213 |
* they are consumed from the stream |
|
17167 | 214 |
* @return the new stream |
215 |
*/ |
|
19850 | 216 |
IntStream peek(IntConsumer action); |
17167 | 217 |
|
218 |
/** |
|
219 |
* Returns a stream consisting of the elements of this stream, truncated |
|
220 |
* to be no longer than {@code maxSize} in length. |
|
221 |
* |
|
222 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
223 |
* stateful intermediate operation</a>. |
|
224 |
* |
|
20866
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
225 |
* @apiNote |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
226 |
* While {@code limit()} is generally a cheap operation on sequential |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
227 |
* stream pipelines, it can be quite expensive on ordered parallel pipelines, |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
228 |
* especially for large values of {@code maxSize}, since {@code limit(n)} |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
229 |
* 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:
19850
diff
changeset
|
230 |
* <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:
19850
diff
changeset
|
231 |
* stream source (such as {@link #generate(IntSupplier)}) or removing the |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
232 |
* ordering constraint with {@link #unordered()} may result in significant |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
233 |
* speedups of {@code limit()} in parallel pipelines, if the semantics of |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
234 |
* your situation permit. If consistency with encounter order is required, |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
235 |
* and you are experiencing poor performance or memory utilization with |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
236 |
* {@code limit()} in parallel pipelines, switching to sequential execution |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
237 |
* with {@link #sequential()} may improve performance. |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
238 |
* |
17167 | 239 |
* @param maxSize the number of elements the stream should be limited to |
240 |
* @return the new stream |
|
241 |
* @throws IllegalArgumentException if {@code maxSize} is negative |
|
242 |
*/ |
|
243 |
IntStream limit(long maxSize); |
|
244 |
||
245 |
/** |
|
246 |
* 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:
19850
diff
changeset
|
247 |
* after discarding the first {@code n} elements of the stream. |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
248 |
* If this stream contains fewer than {@code n} elements then an |
17167 | 249 |
* empty stream will be returned. |
250 |
* |
|
251 |
* <p>This is a <a href="package-summary.html#StreamOps">stateful |
|
252 |
* intermediate operation</a>. |
|
253 |
* |
|
20866
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
254 |
* @apiNote |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
255 |
* While {@code skip()} is generally a cheap operation on sequential |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
256 |
* stream pipelines, it can be quite expensive on ordered parallel pipelines, |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
257 |
* especially for large values of {@code n}, since {@code skip(n)} |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
258 |
* 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:
19850
diff
changeset
|
259 |
* <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:
19850
diff
changeset
|
260 |
* stream source (such as {@link #generate(IntSupplier)}) or removing the |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
261 |
* ordering constraint with {@link #unordered()} may result in significant |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
262 |
* speedups of {@code skip()} in parallel pipelines, if the semantics of |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
263 |
* your situation permit. If consistency with encounter order is required, |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
264 |
* and you are experiencing poor performance or memory utilization with |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
265 |
* {@code skip()} in parallel pipelines, switching to sequential execution |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
266 |
* with {@link #sequential()} may improve performance. |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
267 |
* |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
268 |
* @param n the number of leading elements to skip |
17167 | 269 |
* @return the new stream |
20866
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
270 |
* @throws IllegalArgumentException if {@code n} is negative |
17167 | 271 |
*/ |
20866
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
272 |
IntStream skip(long n); |
17167 | 273 |
|
274 |
/** |
|
275 |
* Performs an action for each element of this stream. |
|
276 |
* |
|
277 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
278 |
* operation</a>. |
|
279 |
* |
|
280 |
* <p>For parallel stream pipelines, this operation does <em>not</em> |
|
281 |
* guarantee to respect the encounter order of the stream, as doing so |
|
282 |
* would sacrifice the benefit of parallelism. For any given element, the |
|
283 |
* action may be performed at whatever time and in whatever thread the |
|
284 |
* library chooses. If the action accesses shared state, it is |
|
285 |
* responsible for providing the required synchronization. |
|
286 |
* |
|
287 |
* @param action a <a href="package-summary.html#NonInterference"> |
|
288 |
* non-interfering</a> action to perform on the elements |
|
289 |
*/ |
|
290 |
void forEach(IntConsumer action); |
|
291 |
||
292 |
/** |
|
293 |
* Performs an action for each element of this stream, guaranteeing that |
|
294 |
* each element is processed in encounter order for streams that have a |
|
295 |
* defined encounter order. |
|
296 |
* |
|
297 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
298 |
* operation</a>. |
|
299 |
* |
|
300 |
* @param action a <a href="package-summary.html#NonInterference"> |
|
301 |
* non-interfering</a> action to perform on the elements |
|
302 |
* @see #forEach(IntConsumer) |
|
303 |
*/ |
|
304 |
void forEachOrdered(IntConsumer action); |
|
305 |
||
306 |
/** |
|
307 |
* Returns an array containing the elements of this stream. |
|
308 |
* |
|
309 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
310 |
* operation</a>. |
|
311 |
* |
|
312 |
* @return an array containing the elements of this stream |
|
313 |
*/ |
|
314 |
int[] toArray(); |
|
315 |
||
316 |
/** |
|
317 |
* Performs a <a href="package-summary.html#Reduction">reduction</a> on the |
|
318 |
* elements of this stream, using the provided identity value and an |
|
319 |
* <a href="package-summary.html#Associativity">associative</a> |
|
320 |
* accumulation function, and returns the reduced value. This is equivalent |
|
321 |
* to: |
|
322 |
* <pre>{@code |
|
323 |
* int result = identity; |
|
324 |
* for (int element : this stream) |
|
325 |
* result = accumulator.apply(result, element) |
|
326 |
* return result; |
|
327 |
* }</pre> |
|
328 |
* |
|
329 |
* but is not constrained to execute sequentially. |
|
330 |
* |
|
331 |
* <p>The {@code identity} value must be an identity for the accumulator |
|
332 |
* function. This means that for all {@code x}, |
|
333 |
* {@code accumulator.apply(identity, x)} is equal to {@code x}. |
|
334 |
* The {@code accumulator} function must be an |
|
335 |
* <a href="package-summary.html#Associativity">associative</a> function. |
|
336 |
* |
|
337 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
338 |
* operation</a>. |
|
339 |
* |
|
340 |
* @apiNote Sum, min, max, and average are all special cases of reduction. |
|
341 |
* Summing a stream of numbers can be expressed as: |
|
342 |
* |
|
343 |
* <pre>{@code |
|
344 |
* int sum = integers.reduce(0, (a, b) -> a+b); |
|
345 |
* }</pre> |
|
346 |
* |
|
347 |
* or more compactly: |
|
348 |
* |
|
349 |
* <pre>{@code |
|
350 |
* int sum = integers.reduce(0, Integer::sum); |
|
351 |
* }</pre> |
|
352 |
* |
|
353 |
* <p>While this may seem a more roundabout way to perform an aggregation |
|
354 |
* compared to simply mutating a running total in a loop, reduction |
|
355 |
* operations parallelize more gracefully, without needing additional |
|
356 |
* synchronization and with greatly reduced risk of data races. |
|
357 |
* |
|
358 |
* @param identity the identity value for the accumulating function |
|
21339 | 359 |
* @param op an <a href="package-summary.html#Associativity">associative</a>, |
360 |
* <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
361 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
362 |
* function for combining two values |
|
17167 | 363 |
* @return the result of the reduction |
364 |
* @see #sum() |
|
365 |
* @see #min() |
|
366 |
* @see #max() |
|
367 |
* @see #average() |
|
368 |
*/ |
|
369 |
int reduce(int identity, IntBinaryOperator op); |
|
370 |
||
371 |
/** |
|
372 |
* Performs a <a href="package-summary.html#Reduction">reduction</a> on the |
|
373 |
* elements of this stream, using an |
|
374 |
* <a href="package-summary.html#Associativity">associative</a> accumulation |
|
375 |
* function, and returns an {@code OptionalInt} describing the reduced value, |
|
376 |
* if any. This is equivalent to: |
|
377 |
* <pre>{@code |
|
378 |
* boolean foundAny = false; |
|
379 |
* int result = null; |
|
380 |
* for (int element : this stream) { |
|
381 |
* if (!foundAny) { |
|
382 |
* foundAny = true; |
|
383 |
* result = element; |
|
384 |
* } |
|
385 |
* else |
|
386 |
* result = accumulator.apply(result, element); |
|
387 |
* } |
|
388 |
* return foundAny ? OptionalInt.of(result) : OptionalInt.empty(); |
|
389 |
* }</pre> |
|
390 |
* |
|
391 |
* but is not constrained to execute sequentially. |
|
392 |
* |
|
393 |
* <p>The {@code accumulator} function must be an |
|
394 |
* <a href="package-summary.html#Associativity">associative</a> function. |
|
395 |
* |
|
396 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
397 |
* operation</a>. |
|
398 |
* |
|
21339 | 399 |
* @param op an <a href="package-summary.html#Associativity">associative</a>, |
400 |
* <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
401 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
402 |
* function for combining two values |
|
17167 | 403 |
* @return the result of the reduction |
404 |
* @see #reduce(int, IntBinaryOperator) |
|
405 |
*/ |
|
406 |
OptionalInt reduce(IntBinaryOperator op); |
|
407 |
||
408 |
/** |
|
409 |
* Performs a <a href="package-summary.html#MutableReduction">mutable |
|
410 |
* reduction</a> operation on the elements of this stream. A mutable |
|
19850 | 411 |
* reduction is one in which the reduced value is a mutable result container, |
17167 | 412 |
* such as an {@code ArrayList}, and elements are incorporated by updating |
19850 | 413 |
* the state of the result rather than by replacing the result. This |
17167 | 414 |
* produces a result equivalent to: |
415 |
* <pre>{@code |
|
19850 | 416 |
* R result = supplier.get(); |
17167 | 417 |
* for (int element : this stream) |
418 |
* accumulator.accept(result, element); |
|
419 |
* return result; |
|
420 |
* }</pre> |
|
421 |
* |
|
422 |
* <p>Like {@link #reduce(int, IntBinaryOperator)}, {@code collect} operations |
|
423 |
* can be parallelized without requiring additional synchronization. |
|
424 |
* |
|
425 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
426 |
* operation</a>. |
|
427 |
* |
|
428 |
* @param <R> type of the result |
|
19850 | 429 |
* @param supplier a function that creates a new result container. For a |
430 |
* parallel execution, this function may be called |
|
431 |
* multiple times and must return a fresh value each time. |
|
21339 | 432 |
* @param accumulator an <a href="package-summary.html#Associativity">associative</a>, |
433 |
* <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
434 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
435 |
* function for incorporating an additional element into a result |
|
436 |
* @param combiner an <a href="package-summary.html#Associativity">associative</a>, |
|
437 |
* <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
438 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
439 |
* function for combining two values, which must be |
|
440 |
* compatible with the accumulator function |
|
17167 | 441 |
* @return the result of the reduction |
442 |
* @see Stream#collect(Supplier, BiConsumer, BiConsumer) |
|
443 |
*/ |
|
19850 | 444 |
<R> R collect(Supplier<R> supplier, |
17167 | 445 |
ObjIntConsumer<R> accumulator, |
446 |
BiConsumer<R, R> combiner); |
|
447 |
||
448 |
/** |
|
449 |
* Returns the sum of elements in this stream. This is a special case |
|
19850 | 450 |
* of a <a href="package-summary.html#Reduction">reduction</a> |
17167 | 451 |
* and is equivalent to: |
452 |
* <pre>{@code |
|
453 |
* return reduce(0, Integer::sum); |
|
454 |
* }</pre> |
|
455 |
* |
|
19850 | 456 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
457 |
* operation</a>. |
|
458 |
* |
|
17167 | 459 |
* @return the sum of elements in this stream |
460 |
*/ |
|
461 |
int sum(); |
|
462 |
||
463 |
/** |
|
464 |
* Returns an {@code OptionalInt} describing the minimum element of this |
|
465 |
* stream, or an empty optional if this stream is empty. This is a special |
|
19850 | 466 |
* case of a <a href="package-summary.html#Reduction">reduction</a> |
17167 | 467 |
* and is equivalent to: |
468 |
* <pre>{@code |
|
469 |
* return reduce(Integer::min); |
|
470 |
* }</pre> |
|
471 |
* |
|
472 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>. |
|
473 |
* |
|
474 |
* @return an {@code OptionalInt} containing the minimum element of this |
|
475 |
* stream, or an empty {@code OptionalInt} if the stream is empty |
|
476 |
*/ |
|
477 |
OptionalInt min(); |
|
478 |
||
479 |
/** |
|
480 |
* Returns an {@code OptionalInt} describing the maximum element of this |
|
481 |
* stream, or an empty optional if this stream is empty. This is a special |
|
19850 | 482 |
* case of a <a href="package-summary.html#Reduction">reduction</a> |
17167 | 483 |
* and is equivalent to: |
484 |
* <pre>{@code |
|
485 |
* return reduce(Integer::max); |
|
486 |
* }</pre> |
|
487 |
* |
|
488 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
489 |
* operation</a>. |
|
490 |
* |
|
491 |
* @return an {@code OptionalInt} containing the maximum element of this |
|
492 |
* stream, or an empty {@code OptionalInt} if the stream is empty |
|
493 |
*/ |
|
494 |
OptionalInt max(); |
|
495 |
||
496 |
/** |
|
497 |
* Returns the count of elements in this stream. This is a special case of |
|
19850 | 498 |
* a <a href="package-summary.html#Reduction">reduction</a> and is |
17167 | 499 |
* equivalent to: |
500 |
* <pre>{@code |
|
501 |
* return mapToLong(e -> 1L).sum(); |
|
502 |
* }</pre> |
|
503 |
* |
|
504 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>. |
|
505 |
* |
|
506 |
* @return the count of elements in this stream |
|
507 |
*/ |
|
508 |
long count(); |
|
509 |
||
510 |
/** |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18825
diff
changeset
|
511 |
* Returns an {@code OptionalDouble} describing the arithmetic mean of elements of |
17167 | 512 |
* this stream, or an empty optional if this stream is empty. This is a |
513 |
* special case of a |
|
19850 | 514 |
* <a href="package-summary.html#Reduction">reduction</a>. |
515 |
* |
|
516 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
517 |
* operation</a>. |
|
17167 | 518 |
* |
519 |
* @return an {@code OptionalDouble} containing the average element of this |
|
520 |
* stream, or an empty optional if the stream is empty |
|
521 |
*/ |
|
522 |
OptionalDouble average(); |
|
523 |
||
524 |
/** |
|
525 |
* Returns an {@code IntSummaryStatistics} describing various |
|
526 |
* summary data about the elements of this stream. This is a special |
|
19850 | 527 |
* case of a <a href="package-summary.html#Reduction">reduction</a>. |
528 |
* |
|
529 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
530 |
* operation</a>. |
|
17167 | 531 |
* |
532 |
* @return an {@code IntSummaryStatistics} describing various summary data |
|
533 |
* about the elements of this stream |
|
534 |
*/ |
|
535 |
IntSummaryStatistics summaryStatistics(); |
|
536 |
||
537 |
/** |
|
538 |
* Returns whether any elements of this stream match the provided |
|
539 |
* predicate. May not evaluate the predicate on all elements if not |
|
21339 | 540 |
* necessary for determining the result. If the stream is empty then |
541 |
* {@code false} is returned and the predicate is not evaluated. |
|
17167 | 542 |
* |
543 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
544 |
* terminal operation</a>. |
|
545 |
* |
|
21339 | 546 |
* @apiNote |
547 |
* This method evaluates the <em>existential quantification</em> of the |
|
548 |
* predicate over the elements of the stream (for some x P(x)). |
|
549 |
* |
|
550 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
551 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
552 |
* predicate to apply to elements of this stream |
|
17167 | 553 |
* @return {@code true} if any elements of the stream match the provided |
21339 | 554 |
* predicate, otherwise {@code false} |
17167 | 555 |
*/ |
556 |
boolean anyMatch(IntPredicate predicate); |
|
557 |
||
558 |
/** |
|
559 |
* Returns whether all elements of this stream match the provided predicate. |
|
560 |
* May not evaluate the predicate on all elements if not necessary for |
|
21339 | 561 |
* determining the result. If the stream is empty then {@code true} is |
562 |
* returned and the predicate is not evaluated. |
|
17167 | 563 |
* |
564 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
565 |
* terminal operation</a>. |
|
566 |
* |
|
21339 | 567 |
* @apiNote |
568 |
* This method evaluates the <em>universal quantification</em> of the |
|
569 |
* predicate over the elements of the stream (for all x P(x)). If the |
|
570 |
* stream is empty, the quantification is said to be <em>vacuously |
|
571 |
* satisfied</em> and is always {@code true} (regardless of P(x)). |
|
572 |
* |
|
573 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
574 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
575 |
* predicate to apply to elements of this stream |
|
576 |
* @return {@code true} if either all elements of the stream match the |
|
577 |
* provided predicate or the stream is empty, otherwise {@code false} |
|
17167 | 578 |
*/ |
579 |
boolean allMatch(IntPredicate predicate); |
|
580 |
||
581 |
/** |
|
582 |
* Returns whether no elements of this stream match the provided predicate. |
|
583 |
* May not evaluate the predicate on all elements if not necessary for |
|
21339 | 584 |
* determining the result. If the stream is empty then {@code true} is |
585 |
* returned and the predicate is not evaluated. |
|
17167 | 586 |
* |
587 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
588 |
* terminal operation</a>. |
|
589 |
* |
|
21339 | 590 |
* @apiNote |
591 |
* This method evaluates the <em>universal quantification</em> of the |
|
592 |
* negated predicate over the elements of the stream (for all x ~P(x)). If |
|
593 |
* the stream is empty, the quantification is said to be vacuously satisfied |
|
594 |
* and is always {@code true}, regardless of P(x). |
|
595 |
* |
|
596 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
597 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
598 |
* predicate to apply to elements of this stream |
|
599 |
* @return {@code true} if either no elements of the stream match the |
|
600 |
* provided predicate or the stream is empty, otherwise {@code false} |
|
17167 | 601 |
*/ |
602 |
boolean noneMatch(IntPredicate predicate); |
|
603 |
||
604 |
/** |
|
605 |
* Returns an {@link OptionalInt} describing the first element of this |
|
19850 | 606 |
* stream, or an empty {@code OptionalInt} if the stream is empty. If the |
607 |
* stream has no encounter order, then any element may be returned. |
|
17167 | 608 |
* |
609 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
610 |
* terminal operation</a>. |
|
611 |
* |
|
612 |
* @return an {@code OptionalInt} describing the first element of this stream, |
|
613 |
* or an empty {@code OptionalInt} if the stream is empty |
|
614 |
*/ |
|
615 |
OptionalInt findFirst(); |
|
616 |
||
617 |
/** |
|
618 |
* Returns an {@link OptionalInt} describing some element of the stream, or |
|
619 |
* an empty {@code OptionalInt} if the stream is empty. |
|
620 |
* |
|
621 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
622 |
* terminal operation</a>. |
|
623 |
* |
|
624 |
* <p>The behavior of this operation is explicitly nondeterministic; it is |
|
625 |
* free to select any element in the stream. This is to allow for maximal |
|
626 |
* performance in parallel operations; the cost is that multiple invocations |
|
19850 | 627 |
* on the same source may not return the same result. (If a stable result |
628 |
* is desired, use {@link #findFirst()} instead.) |
|
17167 | 629 |
* |
630 |
* @return an {@code OptionalInt} describing some element of this stream, or |
|
631 |
* an empty {@code OptionalInt} if the stream is empty |
|
632 |
* @see #findFirst() |
|
633 |
*/ |
|
634 |
OptionalInt findAny(); |
|
635 |
||
636 |
/** |
|
637 |
* Returns a {@code LongStream} consisting of the elements of this stream, |
|
638 |
* converted to {@code long}. |
|
639 |
* |
|
19850 | 640 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
641 |
* operation</a>. |
|
642 |
* |
|
17167 | 643 |
* @return a {@code LongStream} consisting of the elements of this stream, |
644 |
* converted to {@code long} |
|
645 |
*/ |
|
18154
5ede18269905
8015798: Rename IntStream.longs/doubles and LongStream.doubles to asXxxStream
psandoz
parents:
17914
diff
changeset
|
646 |
LongStream asLongStream(); |
17167 | 647 |
|
648 |
/** |
|
649 |
* Returns a {@code DoubleStream} consisting of the elements of this stream, |
|
650 |
* converted to {@code double}. |
|
651 |
* |
|
19850 | 652 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
653 |
* operation</a>. |
|
654 |
* |
|
17167 | 655 |
* @return a {@code DoubleStream} consisting of the elements of this stream, |
656 |
* converted to {@code double} |
|
657 |
*/ |
|
18154
5ede18269905
8015798: Rename IntStream.longs/doubles and LongStream.doubles to asXxxStream
psandoz
parents:
17914
diff
changeset
|
658 |
DoubleStream asDoubleStream(); |
17167 | 659 |
|
660 |
/** |
|
661 |
* Returns a {@code Stream} consisting of the elements of this stream, |
|
662 |
* each boxed to an {@code Integer}. |
|
663 |
* |
|
19850 | 664 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
665 |
* operation</a>. |
|
666 |
* |
|
17167 | 667 |
* @return a {@code Stream} consistent of the elements of this stream, |
668 |
* each boxed to an {@code Integer} |
|
669 |
*/ |
|
670 |
Stream<Integer> boxed(); |
|
671 |
||
672 |
@Override |
|
673 |
IntStream sequential(); |
|
674 |
||
675 |
@Override |
|
676 |
IntStream parallel(); |
|
677 |
||
678 |
@Override |
|
679 |
PrimitiveIterator.OfInt iterator(); |
|
680 |
||
681 |
@Override |
|
682 |
Spliterator.OfInt spliterator(); |
|
17195 | 683 |
|
684 |
// Static factories |
|
685 |
||
686 |
/** |
|
687 |
* Returns a builder for an {@code IntStream}. |
|
688 |
* |
|
689 |
* @return a stream builder |
|
690 |
*/ |
|
18825
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
691 |
public static Builder builder() { |
17195 | 692 |
return new Streams.IntStreamBuilderImpl(); |
693 |
} |
|
694 |
||
695 |
/** |
|
696 |
* Returns an empty sequential {@code IntStream}. |
|
697 |
* |
|
698 |
* @return an empty sequential stream |
|
699 |
*/ |
|
700 |
public static IntStream empty() { |
|
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
701 |
return StreamSupport.intStream(Spliterators.emptyIntSpliterator(), false); |
17195 | 702 |
} |
703 |
||
704 |
/** |
|
705 |
* Returns a sequential {@code IntStream} containing a single element. |
|
706 |
* |
|
707 |
* @param t the single element |
|
708 |
* @return a singleton sequential stream |
|
709 |
*/ |
|
710 |
public static IntStream of(int t) { |
|
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
711 |
return StreamSupport.intStream(new Streams.IntStreamBuilderImpl(t), false); |
17195 | 712 |
} |
713 |
||
714 |
/** |
|
19850 | 715 |
* Returns a sequential ordered stream whose elements are the specified values. |
17195 | 716 |
* |
717 |
* @param values the elements of the new stream |
|
718 |
* @return the new stream |
|
719 |
*/ |
|
720 |
public static IntStream of(int... values) { |
|
721 |
return Arrays.stream(values); |
|
722 |
} |
|
723 |
||
724 |
/** |
|
19850 | 725 |
* Returns an infinite sequential ordered {@code IntStream} produced by iterative |
17195 | 726 |
* application of a function {@code f} to an initial element {@code seed}, |
727 |
* producing a {@code Stream} consisting of {@code seed}, {@code f(seed)}, |
|
728 |
* {@code f(f(seed))}, etc. |
|
729 |
* |
|
730 |
* <p>The first element (position {@code 0}) in the {@code IntStream} will be |
|
731 |
* the provided {@code seed}. For {@code n > 0}, the element at position |
|
732 |
* {@code n}, will be the result of applying the function {@code f} to the |
|
733 |
* element at position {@code n - 1}. |
|
734 |
* |
|
735 |
* @param seed the initial element |
|
736 |
* @param f a function to be applied to to the previous element to produce |
|
737 |
* a new element |
|
738 |
* @return A new sequential {@code IntStream} |
|
739 |
*/ |
|
740 |
public static IntStream iterate(final int seed, final IntUnaryOperator f) { |
|
741 |
Objects.requireNonNull(f); |
|
742 |
final PrimitiveIterator.OfInt iterator = new PrimitiveIterator.OfInt() { |
|
743 |
int t = seed; |
|
744 |
||
745 |
@Override |
|
746 |
public boolean hasNext() { |
|
747 |
return true; |
|
748 |
} |
|
749 |
||
750 |
@Override |
|
751 |
public int nextInt() { |
|
752 |
int v = t; |
|
753 |
t = f.applyAsInt(t); |
|
754 |
return v; |
|
755 |
} |
|
756 |
}; |
|
757 |
return StreamSupport.intStream(Spliterators.spliteratorUnknownSize( |
|
758 |
iterator, |
|
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
759 |
Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false); |
17195 | 760 |
} |
761 |
||
762 |
/** |
|
21339 | 763 |
* Returns an infinite sequential unordered stream where each element is |
764 |
* generated by the provided {@code IntSupplier}. This is suitable for |
|
765 |
* generating constant streams, streams of random elements, etc. |
|
17195 | 766 |
* |
767 |
* @param s the {@code IntSupplier} for generated elements |
|
21339 | 768 |
* @return a new infinite sequential unordered {@code IntStream} |
17195 | 769 |
*/ |
770 |
public static IntStream generate(IntSupplier s) { |
|
771 |
Objects.requireNonNull(s); |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
18158
diff
changeset
|
772 |
return StreamSupport.intStream( |
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
773 |
new StreamSpliterators.InfiniteSupplyingSpliterator.OfInt(Long.MAX_VALUE, s), false); |
17195 | 774 |
} |
775 |
||
776 |
/** |
|
19850 | 777 |
* Returns a sequential ordered {@code IntStream} from {@code startInclusive} |
17195 | 778 |
* (inclusive) to {@code endExclusive} (exclusive) by an incremental step of |
18158 | 779 |
* {@code 1}. |
17195 | 780 |
* |
18158 | 781 |
* @apiNote |
782 |
* <p>An equivalent sequence of increasing values can be produced |
|
783 |
* sequentially using a {@code for} loop as follows: |
|
17195 | 784 |
* <pre>{@code |
18158 | 785 |
* for (int i = startInclusive; i < endExclusive ; i++) { ... } |
17195 | 786 |
* }</pre> |
787 |
* |
|
788 |
* @param startInclusive the (inclusive) initial value |
|
789 |
* @param endExclusive the exclusive upper bound |
|
790 |
* @return a sequential {@code IntStream} for the range of {@code int} |
|
791 |
* elements |
|
792 |
*/ |
|
793 |
public static IntStream range(int startInclusive, int endExclusive) { |
|
18158 | 794 |
if (startInclusive >= endExclusive) { |
795 |
return empty(); |
|
796 |
} else { |
|
797 |
return StreamSupport.intStream( |
|
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
798 |
new Streams.RangeIntSpliterator(startInclusive, endExclusive, false), false); |
18158 | 799 |
} |
17195 | 800 |
} |
801 |
||
802 |
/** |
|
19850 | 803 |
* Returns a sequential ordered {@code IntStream} from {@code startInclusive} |
18158 | 804 |
* (inclusive) to {@code endInclusive} (inclusive) by an incremental step of |
805 |
* {@code 1}. |
|
17195 | 806 |
* |
18158 | 807 |
* @apiNote |
17195 | 808 |
* <p>An equivalent sequence of increasing values can be produced |
809 |
* sequentially using a {@code for} loop as follows: |
|
810 |
* <pre>{@code |
|
18158 | 811 |
* for (int i = startInclusive; i <= endInclusive ; i++) { ... } |
17195 | 812 |
* }</pre> |
813 |
* |
|
814 |
* @param startInclusive the (inclusive) initial value |
|
18158 | 815 |
* @param endInclusive the inclusive upper bound |
17195 | 816 |
* @return a sequential {@code IntStream} for the range of {@code int} |
817 |
* elements |
|
818 |
*/ |
|
18158 | 819 |
public static IntStream rangeClosed(int startInclusive, int endInclusive) { |
820 |
if (startInclusive > endInclusive) { |
|
17195 | 821 |
return empty(); |
822 |
} else { |
|
18158 | 823 |
return StreamSupport.intStream( |
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
824 |
new Streams.RangeIntSpliterator(startInclusive, endInclusive, true), false); |
17195 | 825 |
} |
826 |
} |
|
18820 | 827 |
|
828 |
/** |
|
19850 | 829 |
* Creates a lazily concatenated stream whose elements are all the |
830 |
* elements of the first stream followed by all the elements of the |
|
21339 | 831 |
* second stream. The resulting stream is ordered if both |
18820 | 832 |
* of the input streams are ordered, and parallel if either of the input |
19800 | 833 |
* streams is parallel. When the resulting stream is closed, the close |
834 |
* handlers for both input streams are invoked. |
|
18820 | 835 |
* |
21339 | 836 |
* @implNote |
837 |
* Use caution when constructing streams from repeated concatenation. |
|
838 |
* Accessing an element of a deeply concatenated stream can result in deep |
|
839 |
* call chains, or even {@code StackOverflowException}. |
|
840 |
* |
|
18820 | 841 |
* @param a the first stream |
19850 | 842 |
* @param b the second stream |
843 |
* @return the concatenation of the two input streams |
|
18820 | 844 |
*/ |
845 |
public static IntStream concat(IntStream a, IntStream b) { |
|
846 |
Objects.requireNonNull(a); |
|
847 |
Objects.requireNonNull(b); |
|
848 |
||
849 |
Spliterator.OfInt split = new Streams.ConcatSpliterator.OfInt( |
|
850 |
a.spliterator(), b.spliterator()); |
|
19800 | 851 |
IntStream stream = StreamSupport.intStream(split, a.isParallel() || b.isParallel()); |
852 |
return stream.onClose(Streams.composedClose(a, b)); |
|
18820 | 853 |
} |
18825
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
854 |
|
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
855 |
/** |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
856 |
* A mutable builder for an {@code IntStream}. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
857 |
* |
19850 | 858 |
* <p>A stream builder has a lifecycle, which starts in a building |
859 |
* phase, during which elements can be added, and then transitions to a built |
|
860 |
* phase, after which elements may not be added. The built phase |
|
18825
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
861 |
* begins when the {@link #build()} method is called, which creates an |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
862 |
* ordered stream whose elements are the elements that were added to the |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
863 |
* stream builder, in the order they were added. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
864 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
865 |
* @see IntStream#builder() |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
866 |
* @since 1.8 |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
867 |
*/ |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
868 |
public interface Builder extends IntConsumer { |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
869 |
|
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
870 |
/** |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
871 |
* Adds an element to the stream being built. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
872 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
873 |
* @throws IllegalStateException if the builder has already transitioned |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
874 |
* to the built state |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
875 |
*/ |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
876 |
@Override |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
877 |
void accept(int t); |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
878 |
|
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
879 |
/** |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
880 |
* Adds an element to the stream being built. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
881 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
882 |
* @implSpec |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
883 |
* The default implementation behaves as if: |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
884 |
* <pre>{@code |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
885 |
* accept(t) |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
886 |
* return this; |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
887 |
* }</pre> |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
888 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
889 |
* @param t the element to add |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
890 |
* @return {@code this} builder |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
891 |
* @throws IllegalStateException if the builder has already transitioned |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
892 |
* to the built state |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
893 |
*/ |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
894 |
default Builder add(int t) { |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
895 |
accept(t); |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
896 |
return this; |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
897 |
} |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
898 |
|
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
899 |
/** |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
900 |
* Builds the stream, transitioning this builder to the built state. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
901 |
* An {@code IllegalStateException} is thrown if there are further |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
902 |
* attempts to operate on the builder after it has entered the built |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
903 |
* state. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
904 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
905 |
* @return the built stream |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
906 |
* @throws IllegalStateException if the builder has already transitioned to |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
907 |
* the built state |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
908 |
*/ |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
909 |
IntStream build(); |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
910 |
} |
17167 | 911 |
} |