changeset 19850 | 93b368e54c1c |
parent 19800 | 6e1fef53ea55 |
child 20866 | 36155ee613ef |
19849:49c0b1bda5db | 19850:93b368e54c1c |
---|---|
22 * or visit www.oracle.com if you need additional information or have any |
22 * or visit www.oracle.com if you need additional information or have any |
23 * questions. |
23 * questions. |
24 */ |
24 */ |
25 package java.util.stream; |
25 package java.util.stream; |
26 |
26 |
27 import java.nio.charset.Charset; |
|
28 import java.nio.file.Files; |
|
29 import java.nio.file.Path; |
|
27 import java.util.Arrays; |
30 import java.util.Arrays; |
31 import java.util.Collection; |
|
28 import java.util.IntSummaryStatistics; |
32 import java.util.IntSummaryStatistics; |
29 import java.util.Objects; |
33 import java.util.Objects; |
30 import java.util.OptionalDouble; |
34 import java.util.OptionalDouble; |
31 import java.util.OptionalInt; |
35 import java.util.OptionalInt; |
32 import java.util.PrimitiveIterator; |
36 import java.util.PrimitiveIterator; |
33 import java.util.Spliterator; |
37 import java.util.Spliterator; |
34 import java.util.Spliterators; |
38 import java.util.Spliterators; |
39 import java.util.concurrent.ConcurrentHashMap; |
|
35 import java.util.function.BiConsumer; |
40 import java.util.function.BiConsumer; |
36 import java.util.function.Function; |
41 import java.util.function.Function; |
37 import java.util.function.IntBinaryOperator; |
42 import java.util.function.IntBinaryOperator; |
38 import java.util.function.IntConsumer; |
43 import java.util.function.IntConsumer; |
39 import java.util.function.IntFunction; |
44 import java.util.function.IntFunction; |
44 import java.util.function.IntUnaryOperator; |
49 import java.util.function.IntUnaryOperator; |
45 import java.util.function.ObjIntConsumer; |
50 import java.util.function.ObjIntConsumer; |
46 import java.util.function.Supplier; |
51 import java.util.function.Supplier; |
47 |
52 |
48 /** |
53 /** |
49 * A sequence of primitive integer elements supporting sequential and parallel |
54 * A sequence of elements supporting sequential and parallel aggregate |
50 * bulk operations. Streams support lazy intermediate operations (transforming |
55 * operations. The following example illustrates an aggregate operation using |
51 * a stream to another stream) such as {@code filter} and {@code map}, and terminal |
56 * {@link Stream} and {@link IntStream}: |
52 * operations (consuming the contents of a stream to produce a result or |
57 * |
53 * side-effect), such as {@code forEach}, {@code findFirst}, and {@code |
58 * <pre>{@code |
54 * iterator}. Once an operation has been performed on a stream, it |
59 * int sum = widgets.stream() |
55 * is considered <em>consumed</em> and no longer usable for other operations. |
60 * .filter(w -> w.getColor() == RED) |
56 * |
61 * .mapToInt(w -> w.getWeight()) |
57 * <p>For sequential stream pipelines, all operations are performed in the |
62 * .sum(); |
58 * <a href="package-summary.html#Ordering">encounter order</a> of the pipeline |
63 * }</pre> |
59 * source, if the pipeline source has a defined encounter order. |
64 * |
60 * |
65 * In this example, {@code widgets} is a {@code Collection<Widget>}. We create |
61 * <p>For parallel stream pipelines, unless otherwise specified, intermediate |
66 * a stream of {@code Widget} objects via {@link Collection#stream Collection.stream()}, |
62 * stream operations preserve the <a href="package-summary.html#Ordering"> |
67 * filter it to produce a stream containing only the red widgets, and then |
63 * encounter order</a> of their source, and terminal operations |
68 * transform it into a stream of {@code int} values representing the weight of |
64 * respect the encounter order of their source, if the source |
69 * each red widget. Then this stream is summed to produce a total weight. |
65 * has an encounter order. Provided that and parameters to stream operations |
70 * |
66 * satisfy the <a href="package-summary.html#NonInterference">non-interference |
71 * <p>To perform a computation, stream |
67 * requirements</a>, and excepting differences arising from the absence of |
72 * <a href="package-summary.html#StreamOps">operations</a> are composed into a |
68 * a defined encounter order, the result of a stream pipeline should be the |
73 * <em>stream pipeline</em>. A stream pipeline consists of a source (which |
69 * stable across multiple executions of the same operations on the same source. |
74 * might be an array, a collection, a generator function, an IO channel, |
70 * However, the timing and thread in which side-effects occur (for those |
75 * etc), zero or more <em>intermediate operations</em> (which transform a |
71 * operations which are allowed to produce side-effects, such as |
76 * stream into another stream, such as {@link IntStream#filter(IntPredicate)}), and a |
72 * {@link #forEach(IntConsumer)}), are explicitly nondeterministic for parallel |
77 * <em>terminal operation</em> (which produces a result or side-effect, such |
73 * execution of stream pipelines. |
78 * as {@link IntStream#sum()} or {@link IntStream#forEach(IntConsumer)}). |
74 * |
79 * Streams are lazy; computation on the source data is only performed when the |
75 * <p>Unless otherwise noted, passing a {@code null} argument to any stream |
80 * terminal operation is initiated, and source elements are consumed only |
76 * method may result in a {@link NullPointerException}. |
81 * as needed. |
77 * |
82 * |
78 * @apiNote |
83 * <p>Collections and streams, while bearing some superficial similarities, |
79 * Streams are not data structures; they do not manage the storage for their |
84 * have different goals. Collections are primarily concerned with the efficient |
80 * elements, nor do they support access to individual elements. However, |
85 * management of, and access to, their elements. By contrast, streams do not |
81 * you can use the {@link #iterator()} or {@link #spliterator()} operations to |
86 * provide a means to directly access or manipulate their elements, and are |
82 * perform a controlled traversal. |
87 * instead concerned with declaratively describing their source and the |
88 * computational operations which will be performed in aggregate on that source. |
|
89 * However, if the provided stream operations do not offer the desired |
|
90 * functionality, the {@link #iterator()} and {@link #spliterator()} operations |
|
91 * can be used to perform a controlled traversal. |
|
92 * |
|
93 * <p>A stream pipeline, like the "widgets" example above, can be viewed as |
|
94 * a <em>query</em> on the stream source. Unless the source was explicitly |
|
95 * designed for concurrent modification (such as a {@link ConcurrentHashMap}), |
|
96 * unpredictable or erroneous behavior may result from modifying the stream |
|
97 * source while it is being queried. |
|
98 * |
|
99 * <p>Most stream operations accept parameters that describe user-specified |
|
100 * behavior, such as the lambda expression {@code w -> w.getWeight()} passed to |
|
101 * {@code mapToInt} in the example above. Such parameters are always instances |
|
102 * of a <a href="../function/package-summary.html">functional interface</a> such |
|
103 * as {@link java.util.function.Function}, and are often lambda expressions or |
|
104 * method references. These parameters can never be null, should not modify the |
|
105 * stream source, and should be |
|
106 * <a href="package-summary.html#NonInterference">effectively stateless</a> |
|
107 * (their result should not depend on any state that might change during |
|
108 * execution of the stream pipeline.) |
|
109 * |
|
110 * <p>A stream should be operated on (invoking an intermediate or terminal stream |
|
111 * operation) only once. This rules out, for example, "forked" streams, where |
|
112 * the same source feeds two or more pipelines, or multiple traversals of the |
|
113 * same stream. A stream implementation may throw {@link IllegalStateException} |
|
114 * if it detects that the stream is being reused. However, since some stream |
|
115 * operations may return their receiver rather than a new stream object, it may |
|
116 * not be possible to detect reuse in all cases. |
|
117 * |
|
118 * <p>Streams have a {@link #close()} method and implement {@link AutoCloseable}, |
|
119 * but nearly all stream instances do not actually need to be closed after use. |
|
120 * Generally, only streams whose source is an IO channel (such as those returned |
|
121 * by {@link Files#lines(Path, Charset)}) will require closing. Most streams |
|
122 * are backed by collections, arrays, or generating functions, which require no |
|
123 * special resource management. (If a stream does require closing, it can be |
|
124 * declared as a resource in a {@code try}-with-resources statement.) |
|
125 * |
|
126 * <p>Stream pipelines may execute either sequentially or in |
|
127 * <a href="package-summary.html#Parallelism">parallel</a>. This |
|
128 * execution mode is a property of the stream. Streams are created |
|
129 * with an initial choice of sequential or parallel execution. (For example, |
|
130 * {@link Collection#stream() Collection.stream()} creates a sequential stream, |
|
131 * and {@link Collection#parallelStream() Collection.parallelStream()} creates |
|
132 * a parallel one.) This choice of execution mode may be modified by the |
|
133 * {@link #sequential()} or {@link #parallel()} methods, and may be queried with |
|
134 * the {@link #isParallel()} method. |
|
83 * |
135 * |
84 * @since 1.8 |
136 * @since 1.8 |
85 * @see <a href="package-summary.html">java.util.stream</a> |
137 * @see <a href="package-summary.html">java.util.stream</a> |
86 */ |
138 */ |
87 public interface IntStream extends BaseStream<Integer, IntStream> { |
139 public interface IntStream extends BaseStream<Integer, IntStream> { |
158 DoubleStream mapToDouble(IntToDoubleFunction mapper); |
210 DoubleStream mapToDouble(IntToDoubleFunction mapper); |
159 |
211 |
160 /** |
212 /** |
161 * Returns a stream consisting of the results of replacing each element of |
213 * Returns a stream consisting of the results of replacing each element of |
162 * this stream with the contents of the stream produced by applying the |
214 * this stream with the contents of the stream produced by applying the |
163 * provided mapping function to each element. |
215 * provided mapping function to each element. (If the result of the mapping |
216 * function is {@code null}, this is treated as if the result was an empty |
|
217 * stream.) |
|
164 * |
218 * |
165 * <p>This is an <a href="package-summary.html#StreamOps">intermediate |
219 * <p>This is an <a href="package-summary.html#StreamOps">intermediate |
166 * operation</a>. |
220 * operation</a>. |
167 * |
|
168 * @apiNote |
|
169 * The {@code flatMap()} operation has the effect of applying a one-to-many |
|
170 * tranformation to the elements of the stream, and then flattening the |
|
171 * resulting elements into a new stream. For example, if {@code orders} |
|
172 * is a stream of purchase orders, and each purchase order contains a |
|
173 * collection of line items, then the following produces a stream of line |
|
174 * items: |
|
175 * <pre>{@code |
|
176 * orderStream.flatMap(order -> order.getLineItems().stream())... |
|
177 * }</pre> |
|
178 * |
221 * |
179 * @param mapper a <a href="package-summary.html#NonInterference"> |
222 * @param mapper a <a href="package-summary.html#NonInterference"> |
180 * non-interfering, stateless</a> function to apply to |
223 * non-interfering, stateless</a> function to apply to |
181 * each element which produces an {@code IntStream} of new |
224 * each element which produces an {@code IntStream} of new |
182 * values |
225 * values |
222 * @apiNote This method exists mainly to support debugging, where you want |
265 * @apiNote This method exists mainly to support debugging, where you want |
223 * to see the elements as they flow past a certain point in a pipeline: |
266 * to see the elements as they flow past a certain point in a pipeline: |
224 * <pre>{@code |
267 * <pre>{@code |
225 * list.stream() |
268 * list.stream() |
226 * .filter(filteringFunction) |
269 * .filter(filteringFunction) |
227 * .peek(e -> {System.out.println("Filtered value: " + e); }); |
270 * .peek(e -> System.out.println("Filtered value: " + e)); |
228 * .map(mappingFunction) |
271 * .map(mappingFunction) |
229 * .peek(e -> {System.out.println("Mapped value: " + e); }); |
272 * .peek(e -> System.out.println("Mapped value: " + e)); |
230 * .collect(Collectors.toIntSummaryStastistics()); |
273 * .collect(Collectors.toIntSummaryStastistics()); |
231 * }</pre> |
274 * }</pre> |
232 * |
275 * |
233 * @param consumer a <a href="package-summary.html#NonInterference"> |
276 * @param action a <a href="package-summary.html#NonInterference"> |
234 * non-interfering</a> action to perform on the elements as |
277 * non-interfering</a> action to perform on the elements as |
235 * they are consumed from the stream |
278 * they are consumed from the stream |
236 * @return the new stream |
279 * @return the new stream |
237 */ |
280 */ |
238 IntStream peek(IntConsumer consumer); |
281 IntStream peek(IntConsumer action); |
239 |
282 |
240 /** |
283 /** |
241 * Returns a stream consisting of the elements of this stream, truncated |
284 * Returns a stream consisting of the elements of this stream, truncated |
242 * to be no longer than {@code maxSize} in length. |
285 * to be no longer than {@code maxSize} in length. |
243 * |
286 * |
250 */ |
293 */ |
251 IntStream limit(long maxSize); |
294 IntStream limit(long maxSize); |
252 |
295 |
253 /** |
296 /** |
254 * Returns a stream consisting of the remaining elements of this stream |
297 * Returns a stream consisting of the remaining elements of this stream |
255 * after indexing {@code startInclusive} elements into the stream. If the |
298 * after discarding the first {@code startInclusive} elements of the stream. |
256 * {@code startInclusive} index lies past the end of this stream then an |
299 * If this stream contains fewer than {@code startInclusive} elements then an |
257 * empty stream will be returned. |
300 * empty stream will be returned. |
258 * |
301 * |
259 * <p>This is a <a href="package-summary.html#StreamOps">stateful |
302 * <p>This is a <a href="package-summary.html#StreamOps">stateful |
260 * intermediate operation</a>. |
303 * intermediate operation</a>. |
261 * |
304 * |
265 */ |
308 */ |
266 IntStream substream(long startInclusive); |
309 IntStream substream(long startInclusive); |
267 |
310 |
268 /** |
311 /** |
269 * Returns a stream consisting of the remaining elements of this stream |
312 * Returns a stream consisting of the remaining elements of this stream |
270 * after indexing {@code startInclusive} elements into the stream and |
313 * after discarding the first {@code startInclusive} elements and truncating |
271 * truncated to contain no more than {@code endExclusive - startInclusive} |
314 * the result to be no longer than {@code endExclusive - startInclusive} |
272 * elements. If the {@code startInclusive} index lies past the end |
315 * elements in length. If this stream contains fewer than |
273 * of this stream then an empty stream will be returned. |
316 * {@code startInclusive} elements then an empty stream will be returned. |
274 * |
317 * |
275 * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
318 * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
276 * stateful intermediate operation</a>. |
319 * stateful intermediate operation</a>. |
277 * |
320 * |
278 * @param startInclusive the starting position of the substream, inclusive |
321 * @param startInclusive the starting position of the substream, inclusive |
417 OptionalInt reduce(IntBinaryOperator op); |
460 OptionalInt reduce(IntBinaryOperator op); |
418 |
461 |
419 /** |
462 /** |
420 * Performs a <a href="package-summary.html#MutableReduction">mutable |
463 * Performs a <a href="package-summary.html#MutableReduction">mutable |
421 * reduction</a> operation on the elements of this stream. A mutable |
464 * reduction</a> operation on the elements of this stream. A mutable |
422 * reduction is one in which the reduced value is a mutable value holder, |
465 * reduction is one in which the reduced value is a mutable result container, |
423 * such as an {@code ArrayList}, and elements are incorporated by updating |
466 * such as an {@code ArrayList}, and elements are incorporated by updating |
424 * the state of the result, rather than by replacing the result. This |
467 * the state of the result rather than by replacing the result. This |
425 * produces a result equivalent to: |
468 * produces a result equivalent to: |
426 * <pre>{@code |
469 * <pre>{@code |
427 * R result = resultFactory.get(); |
470 * R result = supplier.get(); |
428 * for (int element : this stream) |
471 * for (int element : this stream) |
429 * accumulator.accept(result, element); |
472 * accumulator.accept(result, element); |
430 * return result; |
473 * return result; |
431 * }</pre> |
474 * }</pre> |
432 * |
475 * |
435 * |
478 * |
436 * <p>This is a <a href="package-summary.html#StreamOps">terminal |
479 * <p>This is a <a href="package-summary.html#StreamOps">terminal |
437 * operation</a>. |
480 * operation</a>. |
438 * |
481 * |
439 * @param <R> type of the result |
482 * @param <R> type of the result |
440 * @param resultFactory a function that creates a new result container. |
483 * @param supplier a function that creates a new result container. For a |
441 * For a parallel execution, this function may be |
484 * parallel execution, this function may be called |
442 * called multiple times and must return a fresh value |
485 * multiple times and must return a fresh value each time. |
443 * each time. |
|
444 * @param accumulator an <a href="package-summary.html#Associativity">associative</a> |
486 * @param accumulator an <a href="package-summary.html#Associativity">associative</a> |
445 * <a href="package-summary.html#NonInterference">non-interfering, |
487 * <a href="package-summary.html#NonInterference">non-interfering, |
446 * stateless</a> function for incorporating an additional |
488 * stateless</a> function for incorporating an additional |
447 * element into a result |
489 * element into a result |
448 * @param combiner an <a href="package-summary.html#Associativity">associative</a> |
490 * @param combiner an <a href="package-summary.html#Associativity">associative</a> |
450 * stateless</a> function for combining two values, which |
492 * stateless</a> function for combining two values, which |
451 * must be compatible with the accumulator function |
493 * must be compatible with the accumulator function |
452 * @return the result of the reduction |
494 * @return the result of the reduction |
453 * @see Stream#collect(Supplier, BiConsumer, BiConsumer) |
495 * @see Stream#collect(Supplier, BiConsumer, BiConsumer) |
454 */ |
496 */ |
455 <R> R collect(Supplier<R> resultFactory, |
497 <R> R collect(Supplier<R> supplier, |
456 ObjIntConsumer<R> accumulator, |
498 ObjIntConsumer<R> accumulator, |
457 BiConsumer<R, R> combiner); |
499 BiConsumer<R, R> combiner); |
458 |
500 |
459 /** |
501 /** |
460 * Returns the sum of elements in this stream. This is a special case |
502 * Returns the sum of elements in this stream. This is a special case |
461 * of a <a href="package-summary.html#MutableReduction">reduction</a> |
503 * of a <a href="package-summary.html#Reduction">reduction</a> |
462 * and is equivalent to: |
504 * and is equivalent to: |
463 * <pre>{@code |
505 * <pre>{@code |
464 * return reduce(0, Integer::sum); |
506 * return reduce(0, Integer::sum); |
465 * }</pre> |
507 * }</pre> |
508 * |
|
509 * <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
510 * operation</a>. |
|
466 * |
511 * |
467 * @return the sum of elements in this stream |
512 * @return the sum of elements in this stream |
468 */ |
513 */ |
469 int sum(); |
514 int sum(); |
470 |
515 |
471 /** |
516 /** |
472 * Returns an {@code OptionalInt} describing the minimum element of this |
517 * Returns an {@code OptionalInt} describing the minimum element of this |
473 * stream, or an empty optional if this stream is empty. This is a special |
518 * stream, or an empty optional if this stream is empty. This is a special |
474 * case of a <a href="package-summary.html#MutableReduction">reduction</a> |
519 * case of a <a href="package-summary.html#Reduction">reduction</a> |
475 * and is equivalent to: |
520 * and is equivalent to: |
476 * <pre>{@code |
521 * <pre>{@code |
477 * return reduce(Integer::min); |
522 * return reduce(Integer::min); |
478 * }</pre> |
523 * }</pre> |
479 * |
524 * |
480 * <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>. |
525 * <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>. |
481 * |
526 * |
482 |
|
483 * @return an {@code OptionalInt} containing the minimum element of this |
527 * @return an {@code OptionalInt} containing the minimum element of this |
484 * stream, or an empty {@code OptionalInt} if the stream is empty |
528 * stream, or an empty {@code OptionalInt} if the stream is empty |
485 */ |
529 */ |
486 OptionalInt min(); |
530 OptionalInt min(); |
487 |
531 |
488 /** |
532 /** |
489 * Returns an {@code OptionalInt} describing the maximum element of this |
533 * Returns an {@code OptionalInt} describing the maximum element of this |
490 * stream, or an empty optional if this stream is empty. This is a special |
534 * stream, or an empty optional if this stream is empty. This is a special |
491 * case of a <a href="package-summary.html#MutableReduction">reduction</a> |
535 * case of a <a href="package-summary.html#Reduction">reduction</a> |
492 * and is equivalent to: |
536 * and is equivalent to: |
493 * <pre>{@code |
537 * <pre>{@code |
494 * return reduce(Integer::max); |
538 * return reduce(Integer::max); |
495 * }</pre> |
539 * }</pre> |
496 * |
540 * |
502 */ |
546 */ |
503 OptionalInt max(); |
547 OptionalInt max(); |
504 |
548 |
505 /** |
549 /** |
506 * Returns the count of elements in this stream. This is a special case of |
550 * Returns the count of elements in this stream. This is a special case of |
507 * a <a href="package-summary.html#MutableReduction">reduction</a> and is |
551 * a <a href="package-summary.html#Reduction">reduction</a> and is |
508 * equivalent to: |
552 * equivalent to: |
509 * <pre>{@code |
553 * <pre>{@code |
510 * return mapToLong(e -> 1L).sum(); |
554 * return mapToLong(e -> 1L).sum(); |
511 * }</pre> |
555 * }</pre> |
512 * |
556 * |
518 |
562 |
519 /** |
563 /** |
520 * Returns an {@code OptionalDouble} describing the arithmetic mean of elements of |
564 * Returns an {@code OptionalDouble} describing the arithmetic mean of elements of |
521 * this stream, or an empty optional if this stream is empty. This is a |
565 * this stream, or an empty optional if this stream is empty. This is a |
522 * special case of a |
566 * special case of a |
523 * <a href="package-summary.html#MutableReduction">reduction</a>. |
567 * <a href="package-summary.html#Reduction">reduction</a>. |
568 * |
|
569 * <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
570 * operation</a>. |
|
524 * |
571 * |
525 * @return an {@code OptionalDouble} containing the average element of this |
572 * @return an {@code OptionalDouble} containing the average element of this |
526 * stream, or an empty optional if the stream is empty |
573 * stream, or an empty optional if the stream is empty |
527 */ |
574 */ |
528 OptionalDouble average(); |
575 OptionalDouble average(); |
529 |
576 |
530 /** |
577 /** |
531 * Returns an {@code IntSummaryStatistics} describing various |
578 * Returns an {@code IntSummaryStatistics} describing various |
532 * summary data about the elements of this stream. This is a special |
579 * summary data about the elements of this stream. This is a special |
533 * case of a <a href="package-summary.html#MutableReduction">reduction</a>. |
580 * case of a <a href="package-summary.html#Reduction">reduction</a>. |
581 * |
|
582 * <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
583 * operation</a>. |
|
534 * |
584 * |
535 * @return an {@code IntSummaryStatistics} describing various summary data |
585 * @return an {@code IntSummaryStatistics} describing various summary data |
536 * about the elements of this stream |
586 * about the elements of this stream |
537 */ |
587 */ |
538 IntSummaryStatistics summaryStatistics(); |
588 IntSummaryStatistics summaryStatistics(); |
585 */ |
635 */ |
586 boolean noneMatch(IntPredicate predicate); |
636 boolean noneMatch(IntPredicate predicate); |
587 |
637 |
588 /** |
638 /** |
589 * Returns an {@link OptionalInt} describing the first element of this |
639 * Returns an {@link OptionalInt} describing the first element of this |
590 * stream (in the encounter order), or an empty {@code OptionalInt} if the |
640 * stream, or an empty {@code OptionalInt} if the stream is empty. If the |
591 * stream is empty. If the stream has no encounter order, then any element |
641 * stream has no encounter order, then any element may be returned. |
592 * may be returned. |
|
593 * |
642 * |
594 * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
643 * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
595 * terminal operation</a>. |
644 * terminal operation</a>. |
596 * |
645 * |
597 * @return an {@code OptionalInt} describing the first element of this stream, |
646 * @return an {@code OptionalInt} describing the first element of this stream, |
607 * terminal operation</a>. |
656 * terminal operation</a>. |
608 * |
657 * |
609 * <p>The behavior of this operation is explicitly nondeterministic; it is |
658 * <p>The behavior of this operation is explicitly nondeterministic; it is |
610 * free to select any element in the stream. This is to allow for maximal |
659 * free to select any element in the stream. This is to allow for maximal |
611 * performance in parallel operations; the cost is that multiple invocations |
660 * performance in parallel operations; the cost is that multiple invocations |
612 * on the same source may not return the same result. (If the first element |
661 * on the same source may not return the same result. (If a stable result |
613 * in the encounter order is desired, use {@link #findFirst()} instead.) |
662 * is desired, use {@link #findFirst()} instead.) |
614 * |
663 * |
615 * @return an {@code OptionalInt} describing some element of this stream, or |
664 * @return an {@code OptionalInt} describing some element of this stream, or |
616 * an empty {@code OptionalInt} if the stream is empty |
665 * an empty {@code OptionalInt} if the stream is empty |
617 * @see #findFirst() |
666 * @see #findFirst() |
618 */ |
667 */ |
620 |
669 |
621 /** |
670 /** |
622 * Returns a {@code LongStream} consisting of the elements of this stream, |
671 * Returns a {@code LongStream} consisting of the elements of this stream, |
623 * converted to {@code long}. |
672 * converted to {@code long}. |
624 * |
673 * |
674 * <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
675 * operation</a>. |
|
676 * |
|
625 * @return a {@code LongStream} consisting of the elements of this stream, |
677 * @return a {@code LongStream} consisting of the elements of this stream, |
626 * converted to {@code long} |
678 * converted to {@code long} |
627 */ |
679 */ |
628 LongStream asLongStream(); |
680 LongStream asLongStream(); |
629 |
681 |
630 /** |
682 /** |
631 * Returns a {@code DoubleStream} consisting of the elements of this stream, |
683 * Returns a {@code DoubleStream} consisting of the elements of this stream, |
632 * converted to {@code double}. |
684 * converted to {@code double}. |
633 * |
685 * |
686 * <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
687 * operation</a>. |
|
688 * |
|
634 * @return a {@code DoubleStream} consisting of the elements of this stream, |
689 * @return a {@code DoubleStream} consisting of the elements of this stream, |
635 * converted to {@code double} |
690 * converted to {@code double} |
636 */ |
691 */ |
637 DoubleStream asDoubleStream(); |
692 DoubleStream asDoubleStream(); |
638 |
693 |
639 /** |
694 /** |
640 * Returns a {@code Stream} consisting of the elements of this stream, |
695 * Returns a {@code Stream} consisting of the elements of this stream, |
641 * each boxed to an {@code Integer}. |
696 * each boxed to an {@code Integer}. |
697 * |
|
698 * <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
699 * operation</a>. |
|
642 * |
700 * |
643 * @return a {@code Stream} consistent of the elements of this stream, |
701 * @return a {@code Stream} consistent of the elements of this stream, |
644 * each boxed to an {@code Integer} |
702 * each boxed to an {@code Integer} |
645 */ |
703 */ |
646 Stream<Integer> boxed(); |
704 Stream<Integer> boxed(); |
686 public static IntStream of(int t) { |
744 public static IntStream of(int t) { |
687 return StreamSupport.intStream(new Streams.IntStreamBuilderImpl(t), false); |
745 return StreamSupport.intStream(new Streams.IntStreamBuilderImpl(t), false); |
688 } |
746 } |
689 |
747 |
690 /** |
748 /** |
691 * Returns a sequential stream whose elements are the specified values. |
749 * Returns a sequential ordered stream whose elements are the specified values. |
692 * |
750 * |
693 * @param values the elements of the new stream |
751 * @param values the elements of the new stream |
694 * @return the new stream |
752 * @return the new stream |
695 */ |
753 */ |
696 public static IntStream of(int... values) { |
754 public static IntStream of(int... values) { |
697 return Arrays.stream(values); |
755 return Arrays.stream(values); |
698 } |
756 } |
699 |
757 |
700 /** |
758 /** |
701 * Returns an infinite sequential {@code IntStream} produced by iterative |
759 * Returns an infinite sequential ordered {@code IntStream} produced by iterative |
702 * application of a function {@code f} to an initial element {@code seed}, |
760 * application of a function {@code f} to an initial element {@code seed}, |
703 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)}, |
761 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)}, |
704 * {@code f(f(seed))}, etc. |
762 * {@code f(f(seed))}, etc. |
705 * |
763 * |
706 * <p>The first element (position {@code 0}) in the {@code IntStream} will be |
764 * <p>The first element (position {@code 0}) in the {@code IntStream} will be |
734 iterator, |
792 iterator, |
735 Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false); |
793 Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false); |
736 } |
794 } |
737 |
795 |
738 /** |
796 /** |
739 * Returns a sequential {@code IntStream} where each element is |
797 * Returns a sequential stream where each element is generated by |
740 * generated by an {@code IntSupplier}. This is suitable for generating |
798 * the provided {@code IntSupplier}. This is suitable for generating |
741 * constant streams, streams of random elements, etc. |
799 * constant streams, streams of random elements, etc. |
742 * |
800 * |
743 * @param s the {@code IntSupplier} for generated elements |
801 * @param s the {@code IntSupplier} for generated elements |
744 * @return a new sequential {@code IntStream} |
802 * @return a new sequential {@code IntStream} |
745 */ |
803 */ |
748 return StreamSupport.intStream( |
806 return StreamSupport.intStream( |
749 new StreamSpliterators.InfiniteSupplyingSpliterator.OfInt(Long.MAX_VALUE, s), false); |
807 new StreamSpliterators.InfiniteSupplyingSpliterator.OfInt(Long.MAX_VALUE, s), false); |
750 } |
808 } |
751 |
809 |
752 /** |
810 /** |
753 * Returns a sequential {@code IntStream} from {@code startInclusive} |
811 * Returns a sequential ordered {@code IntStream} from {@code startInclusive} |
754 * (inclusive) to {@code endExclusive} (exclusive) by an incremental step of |
812 * (inclusive) to {@code endExclusive} (exclusive) by an incremental step of |
755 * {@code 1}. |
813 * {@code 1}. |
756 * |
814 * |
757 * @apiNote |
815 * @apiNote |
758 * <p>An equivalent sequence of increasing values can be produced |
816 * <p>An equivalent sequence of increasing values can be produced |
774 new Streams.RangeIntSpliterator(startInclusive, endExclusive, false), false); |
832 new Streams.RangeIntSpliterator(startInclusive, endExclusive, false), false); |
775 } |
833 } |
776 } |
834 } |
777 |
835 |
778 /** |
836 /** |
779 * Returns a sequential {@code IntStream} from {@code startInclusive} |
837 * Returns a sequential ordered {@code IntStream} from {@code startInclusive} |
780 * (inclusive) to {@code endInclusive} (inclusive) by an incremental step of |
838 * (inclusive) to {@code endInclusive} (inclusive) by an incremental step of |
781 * {@code 1}. |
839 * {@code 1}. |
782 * |
840 * |
783 * @apiNote |
841 * @apiNote |
784 * <p>An equivalent sequence of increasing values can be produced |
842 * <p>An equivalent sequence of increasing values can be produced |
800 new Streams.RangeIntSpliterator(startInclusive, endInclusive, true), false); |
858 new Streams.RangeIntSpliterator(startInclusive, endInclusive, true), false); |
801 } |
859 } |
802 } |
860 } |
803 |
861 |
804 /** |
862 /** |
805 * Creates a lazy concatenated {@code IntStream} whose elements are all the |
863 * Creates a lazily concatenated stream whose elements are all the |
806 * elements of a first {@code IntStream} succeeded by all the elements of the |
864 * elements of the first stream followed by all the elements of the |
807 * second {@code IntStream}. The resulting stream is ordered if both |
865 * second stream. The resulting stream is ordered if both |
808 * of the input streams are ordered, and parallel if either of the input |
866 * of the input streams are ordered, and parallel if either of the input |
809 * streams is parallel. When the resulting stream is closed, the close |
867 * streams is parallel. When the resulting stream is closed, the close |
810 * handlers for both input streams are invoked. |
868 * handlers for both input streams are invoked. |
811 * |
869 * |
812 * @param a the first stream |
870 * @param a the first stream |
813 * @param b the second stream to concatenate on to end of the first stream |
871 * @param b the second stream |
814 * @return the concatenation of the two streams |
872 * @return the concatenation of the two input streams |
815 */ |
873 */ |
816 public static IntStream concat(IntStream a, IntStream b) { |
874 public static IntStream concat(IntStream a, IntStream b) { |
817 Objects.requireNonNull(a); |
875 Objects.requireNonNull(a); |
818 Objects.requireNonNull(b); |
876 Objects.requireNonNull(b); |
819 |
877 |
824 } |
882 } |
825 |
883 |
826 /** |
884 /** |
827 * A mutable builder for an {@code IntStream}. |
885 * A mutable builder for an {@code IntStream}. |
828 * |
886 * |
829 * <p>A stream builder has a lifecycle, where it starts in a building |
887 * <p>A stream builder has a lifecycle, which starts in a building |
830 * phase, during which elements can be added, and then transitions to a |
888 * phase, during which elements can be added, and then transitions to a built |
831 * built phase, after which elements may not be added. The built phase |
889 * phase, after which elements may not be added. The built phase |
832 * begins when the {@link #build()} method is called, which creates an |
890 * begins when the {@link #build()} method is called, which creates an |
833 * ordered stream whose elements are the elements that were added to the |
891 * ordered stream whose elements are the elements that were added to the |
834 * stream builder, in the order they were added. |
892 * stream builder, in the order they were added. |
835 * |
893 * |
836 * @see IntStream#builder() |
894 * @see IntStream#builder() |