1 /* 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package java.util.stream; 26 27 import java.util.Arrays; 28 import java.util.IntSummaryStatistics; 29 import java.util.Objects; 30 import java.util.OptionalDouble; 31 import java.util.OptionalInt; 32 import java.util.PrimitiveIterator; 33 import java.util.Spliterator; 34 import java.util.Spliterators; 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; 41 import java.util.function.IntSupplier; 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 /** 49 * A sequence of primitive integer elements supporting sequential and parallel 50 * bulk operations. Streams support lazy intermediate operations (transforming 51 * a stream to another stream) such as {@code filter} and {@code map}, and terminal 52 * operations (consuming the contents of a stream to produce a result or 53 * side-effect), such as {@code forEach}, {@code findFirst}, and {@code 54 * iterator}. Once an operation has been performed on a stream, it 55 * is considered <em>consumed</em> and no longer usable for other operations. 56 * 57 * <p>For sequential stream pipelines, all operations are performed in the 58 * <a href="package-summary.html#Ordering">encounter order</a> of the pipeline 59 * source, if the pipeline source has a defined encounter order. 60 * 61 * <p>For parallel stream pipelines, unless otherwise specified, intermediate 62 * stream operations preserve the <a href="package-summary.html#Ordering"> 63 * encounter order</a> of their source, and terminal operations 64 * respect the encounter order of their source, if the source 65 * has an encounter order. Provided that and parameters to stream operations 66 * satisfy the <a href="package-summary.html#NonInterference">non-interference 67 * requirements</a>, and excepting differences arising from the absence of 68 * a defined encounter order, the result of a stream pipeline should be the 69 * stable across multiple executions of the same operations on the same source. 70 * However, the timing and thread in which side-effects occur (for those 71 * operations which are allowed to produce side-effects, such as 72 * {@link #forEach(IntConsumer)}), are explicitly nondeterministic for parallel 73 * execution of stream pipelines. 74 * 75 * <p>Unless otherwise noted, passing a {@code null} argument to any stream 76 * method may result in a {@link NullPointerException}. 77 * 78 * @apiNote 79 * Streams are not data structures; they do not manage the storage for their 80 * elements, nor do they support access to individual elements. However, 81 * you can use the {@link #iterator()} or {@link #spliterator()} operations to 82 * perform a controlled traversal. 83 * 84 * @since 1.8 85 * @see <a href="package-summary.html">java.util.stream</a> 86 */ 87 public interface IntStream extends BaseStream<Integer, IntStream> { 88 89 /** 90 * Returns a stream consisting of the elements of this stream that match 91 * the given predicate. 92 * 93 * <p>This is an <a href="package-summary.html#StreamOps">intermediate 94 * operation</a>. 95 * 96 * @param predicate a <a href="package-summary.html#NonInterference"> 97 * non-interfering, stateless</a> predicate to apply to 98 * each element to determine if it should be included 99 * @return the new stream 100 */ 101 IntStream filter(IntPredicate predicate); 102 103 /** 104 * Returns a stream consisting of the results of applying the given 105 * function to the elements of this stream. 106 * 107 * <p>This is an <a href="package-summary.html#StreamOps">intermediate 108 * operation</a>. 109 * 110 * @param mapper a <a href="package-summary.html#NonInterference"> 111 * non-interfering, stateless</a> function to apply to each 112 * element 113 * @return the new stream 114 */ 115 IntStream map(IntUnaryOperator mapper); 116 117 /** 118 * Returns an object-valued {@code Stream} consisting of the results of 119 * applying the given function to the elements of this stream. 120 * 121 * <p>This is an <a href="package-summary.html#StreamOps"> 122 * intermediate operation</a>. 123 * 124 * @param <U> the element type of the new stream 125 * @param mapper a <a href="package-summary.html#NonInterference"> 126 * non-interfering, stateless</a> function to apply to each 127 * element 128 * @return the new stream 129 */ 130 <U> Stream<U> mapToObj(IntFunction<? extends U> mapper); 131 132 /** 133 * Returns a {@code LongStream} consisting of the results of applying the 134 * given function to the elements of this stream. 135 * 136 * <p>This is an <a href="package-summary.html#StreamOps">intermediate 137 * operation</a>. 138 * 139 * @param mapper a <a href="package-summary.html#NonInterference"> 140 * non-interfering, stateless</a> function to apply to each 141 * element 142 * @return the new stream 143 */ 144 LongStream mapToLong(IntToLongFunction mapper); 145 146 /** 147 * Returns a {@code DoubleStream} consisting of the results of applying the 148 * given function to the elements of this stream. 149 * 150 * <p>This is an <a href="package-summary.html#StreamOps">intermediate 151 * operation</a>. 152 * 153 * @param mapper a <a href="package-summary.html#NonInterference"> 154 * non-interfering, stateless</a> function to apply to each 155 * element 156 * @return the new stream 157 */ 158 DoubleStream mapToDouble(IntToDoubleFunction mapper); 159 160 /** 161 * 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 163 * provided mapping function to each element. 164 * 165 * <p>This is an <a href="package-summary.html#StreamOps">intermediate 166 * 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 * 179 * @param mapper a <a href="package-summary.html#NonInterference"> 180 * non-interfering, stateless</a> function to apply to 181 * each element which produces an {@code IntStream} of new 182 * values 183 * @return the new stream 184 * @see Stream#flatMap(Function) 185 */ 186 IntStream flatMap(IntFunction<? extends IntStream> mapper); 187 188 /** 189 * Returns a stream consisting of the distinct elements of this stream. 190 * 191 * <p>This is a <a href="package-summary.html#StreamOps">stateful 192 * intermediate operation</a>. 193 * 194 * @return the new stream 195 */ 196 IntStream distinct(); 197 198 /** 199 * Returns a stream consisting of the elements of this stream in sorted 200 * order. 201 * 202 * <p>This is a <a href="package-summary.html#StreamOps">stateful 203 * intermediate operation</a>. 204 * 205 * @return the new stream 206 */ 207 IntStream sorted(); 208 209 /** 210 * Returns a stream consisting of the elements of this stream, additionally 211 * performing the provided action on each element as elements are consumed 212 * from the resulting stream. 213 * 214 * <p>This is an <a href="package-summary.html#StreamOps">intermediate 215 * operation</a>. 216 * 217 * <p>For parallel stream pipelines, the action may be called at 218 * whatever time and in whatever thread the element is made available by the 219 * upstream operation. If the action modifies shared state, 220 * it is responsible for providing the required synchronization. 221 * 222 * @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: 224 * <pre>{@code 225 * list.stream() 226 * .filter(filteringFunction) 227 * .peek(e -> {System.out.println("Filtered value: " + e); }); 228 * .map(mappingFunction) 229 * .peek(e -> {System.out.println("Mapped value: " + e); }); 230 * .collect(Collectors.toIntSummaryStastistics()); 231 * }</pre> 232 * 233 * @param consumer a <a href="package-summary.html#NonInterference"> 234 * non-interfering</a> action to perform on the elements as 235 * they are consumed from the stream 236 * @return the new stream 237 */ 238 IntStream peek(IntConsumer consumer); 239 240 /** 241 * Returns a stream consisting of the elements of this stream, truncated 242 * to be no longer than {@code maxSize} in length. 243 * 244 * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting 245 * stateful intermediate operation</a>. 246 * 247 * @param maxSize the number of elements the stream should be limited to 248 * @return the new stream 249 * @throws IllegalArgumentException if {@code maxSize} is negative 250 */ 251 IntStream limit(long maxSize); 252 253 /** 254 * Returns a stream consisting of the remaining elements of this stream 255 * after indexing {@code startInclusive} elements into the stream. If the 256 * {@code startInclusive} index lies past the end of this stream then an 257 * empty stream will be returned. 258 * 259 * <p>This is a <a href="package-summary.html#StreamOps">stateful 260 * intermediate operation</a>. 261 * 262 * @param startInclusive the number of leading elements to skip 263 * @return the new stream 264 * @throws IllegalArgumentException if {@code startInclusive} is negative 265 */ 266 IntStream substream(long startInclusive); 267 268 /** 269 * Returns a stream consisting of the remaining elements of this stream 270 * after indexing {@code startInclusive} elements into the stream and 271 * truncated to contain no more than {@code endExclusive - startInclusive} 272 * elements. If the {@code startInclusive} index lies past the end 273 * of this stream then an empty stream will be returned. 274 * 275 * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting 276 * stateful intermediate operation</a>. 277 * 278 * @param startInclusive the starting position of the substream, inclusive 279 * @param endExclusive the ending position of the substream, exclusive 280 * @return the new stream 281 * @throws IllegalArgumentException if {@code startInclusive} or 282 * {@code endExclusive} is negative or {@code startInclusive} is greater 283 * than {@code endExclusive} 284 */ 285 IntStream substream(long startInclusive, long endExclusive); 286 287 /** 288 * Performs an action for each element of this stream. 289 * 290 * <p>This is a <a href="package-summary.html#StreamOps">terminal 291 * operation</a>. 292 * 293 * <p>For parallel stream pipelines, this operation does <em>not</em> 294 * guarantee to respect the encounter order of the stream, as doing so 295 * would sacrifice the benefit of parallelism. For any given element, the 296 * action may be performed at whatever time and in whatever thread the 297 * library chooses. If the action accesses shared state, it is 298 * responsible for providing the required synchronization. 299 * 300 * @param action a <a href="package-summary.html#NonInterference"> 301 * non-interfering</a> action to perform on the elements 302 */ 303 void forEach(IntConsumer action); 304 305 /** 306 * Performs an action for each element of this stream, guaranteeing that 307 * each element is processed in encounter order for streams that have a 308 * defined encounter order. 309 * 310 * <p>This is a <a href="package-summary.html#StreamOps">terminal 311 * operation</a>. 312 * 313 * @param action a <a href="package-summary.html#NonInterference"> 314 * non-interfering</a> action to perform on the elements 315 * @see #forEach(IntConsumer) 316 */ 317 void forEachOrdered(IntConsumer action); 318 319 /** 320 * Returns an array containing the elements of this stream. 321 * 322 * <p>This is a <a href="package-summary.html#StreamOps">terminal 323 * operation</a>. 324 * 325 * @return an array containing the elements of this stream 326 */ 327 int[] toArray(); 328 329 /** 330 * Performs a <a href="package-summary.html#Reduction">reduction</a> on the 331 * elements of this stream, using the provided identity value and an 332 * <a href="package-summary.html#Associativity">associative</a> 333 * accumulation function, and returns the reduced value. This is equivalent 334 * to: 335 * <pre>{@code 336 * int result = identity; 337 * for (int element : this stream) 338 * result = accumulator.apply(result, element) 339 * return result; 340 * }</pre> 341 * 342 * but is not constrained to execute sequentially. 343 * 344 * <p>The {@code identity} value must be an identity for the accumulator 345 * function. This means that for all {@code x}, 346 * {@code accumulator.apply(identity, x)} is equal to {@code x}. 347 * The {@code accumulator} function must be an 348 * <a href="package-summary.html#Associativity">associative</a> function. 349 * 350 * <p>This is a <a href="package-summary.html#StreamOps">terminal 351 * operation</a>. 352 * 353 * @apiNote Sum, min, max, and average are all special cases of reduction. 354 * Summing a stream of numbers can be expressed as: 355 * 356 * <pre>{@code 357 * int sum = integers.reduce(0, (a, b) -> a+b); 358 * }</pre> 359 * 360 * or more compactly: 361 * 362 * <pre>{@code 363 * int sum = integers.reduce(0, Integer::sum); 364 * }</pre> 365 * 366 * <p>While this may seem a more roundabout way to perform an aggregation 367 * compared to simply mutating a running total in a loop, reduction 368 * operations parallelize more gracefully, without needing additional 369 * synchronization and with greatly reduced risk of data races. 370 * 371 * @param identity the identity value for the accumulating function 372 * @param op an <a href="package-summary.html#Associativity">associative</a> 373 * <a href="package-summary.html#NonInterference">non-interfering, 374 * stateless</a> function for combining two values 375 * @return the result of the reduction 376 * @see #sum() 377 * @see #min() 378 * @see #max() 379 * @see #average() 380 */ 381 int reduce(int identity, IntBinaryOperator op); 382 383 /** 384 * Performs a <a href="package-summary.html#Reduction">reduction</a> on the 385 * elements of this stream, using an 386 * <a href="package-summary.html#Associativity">associative</a> accumulation 387 * function, and returns an {@code OptionalInt} describing the reduced value, 388 * if any. This is equivalent to: 389 * <pre>{@code 390 * boolean foundAny = false; 391 * int result = null; 392 * for (int element : this stream) { 393 * if (!foundAny) { 394 * foundAny = true; 395 * result = element; 396 * } 397 * else 398 * result = accumulator.apply(result, element); 399 * } 400 * return foundAny ? OptionalInt.of(result) : OptionalInt.empty(); 401 * }</pre> 402 * 403 * but is not constrained to execute sequentially. 404 * 405 * <p>The {@code accumulator} function must be an 406 * <a href="package-summary.html#Associativity">associative</a> function. 407 * 408 * <p>This is a <a href="package-summary.html#StreamOps">terminal 409 * operation</a>. 410 * 411 * @param op an <a href="package-summary.html#Associativity">associative</a> 412 * <a href="package-summary.html#NonInterference">non-interfering, 413 * stateless</a> function for combining two values 414 * @return the result of the reduction 415 * @see #reduce(int, IntBinaryOperator) 416 */ 417 OptionalInt reduce(IntBinaryOperator op); 418 419 /** 420 * Performs a <a href="package-summary.html#MutableReduction">mutable 421 * 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, 423 * such as an {@code ArrayList}, and elements are incorporated by updating 424 * the state of the result, rather than by replacing the result. This 425 * produces a result equivalent to: 426 * <pre>{@code 427 * R result = resultFactory.get(); 428 * for (int element : this stream) 429 * accumulator.accept(result, element); 430 * return result; 431 * }</pre> 432 * 433 * <p>Like {@link #reduce(int, IntBinaryOperator)}, {@code collect} operations 434 * can be parallelized without requiring additional synchronization. 435 * 436 * <p>This is a <a href="package-summary.html#StreamOps">terminal 437 * operation</a>. 438 * 439 * @param <R> type of the result 440 * @param resultFactory a function that creates a new result container. 441 * For a parallel execution, this function may be 442 * called multiple times and must return a fresh value 443 * each time. 444 * @param accumulator an <a href="package-summary.html#Associativity">associative</a> 445 * <a href="package-summary.html#NonInterference">non-interfering, 446 * stateless</a> function for incorporating an additional 447 * element into a result 448 * @param combiner an <a href="package-summary.html#Associativity">associative</a> 449 * <a href="package-summary.html#NonInterference">non-interfering, 450 * stateless</a> function for combining two values, which 451 * must be compatible with the accumulator function 452 * @return the result of the reduction 453 * @see Stream#collect(Supplier, BiConsumer, BiConsumer) 454 */ 455 <R> R collect(Supplier<R> resultFactory, 456 ObjIntConsumer<R> accumulator, 457 BiConsumer<R, R> combiner); 458 459 /** 460 * Returns the sum of elements in this stream. This is a special case 461 * of a <a href="package-summary.html#MutableReduction">reduction</a> 462 * and is equivalent to: 463 * <pre>{@code 464 * return reduce(0, Integer::sum); 465 * }</pre> 466 * 467 * @return the sum of elements in this stream 468 */ 469 int sum(); 470 471 /** 472 * 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 474 * case of a <a href="package-summary.html#MutableReduction">reduction</a> 475 * and is equivalent to: 476 * <pre>{@code 477 * return reduce(Integer::min); 478 * }</pre> 479 * 480 * <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>. 481 * 482 483 * @return an {@code OptionalInt} containing the minimum element of this 484 * stream, or an empty {@code OptionalInt} if the stream is empty 485 */ 486 OptionalInt min(); 487 488 /** 489 * 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 491 * case of a <a href="package-summary.html#MutableReduction">reduction</a> 492 * and is equivalent to: 493 * <pre>{@code 494 * return reduce(Integer::max); 495 * }</pre> 496 * 497 * <p>This is a <a href="package-summary.html#StreamOps">terminal 498 * operation</a>. 499 * 500 * @return an {@code OptionalInt} containing the maximum element of this 501 * stream, or an empty {@code OptionalInt} if the stream is empty 502 */ 503 OptionalInt max(); 504 505 /** 506 * 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 508 * equivalent to: 509 * <pre>{@code 510 * return mapToLong(e -> 1L).sum(); 511 * }</pre> 512 * 513 * <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>. 514 * 515 * @return the count of elements in this stream 516 */ 517 long count(); 518 519 /** 520 * Returns an {@code OptionalDouble} describing the average of elements of 521 * this stream, or an empty optional if this stream is empty. This is a 522 * special case of a 523 * <a href="package-summary.html#MutableReduction">reduction</a>. 524 * 525 * @return an {@code OptionalDouble} containing the average element of this 526 * stream, or an empty optional if the stream is empty 527 */ 528 OptionalDouble average(); 529 530 /** 531 * Returns an {@code IntSummaryStatistics} describing various 532 * summary data about the elements of this stream. This is a special 533 * case of a <a href="package-summary.html#MutableReduction">reduction</a>. 534 * 535 * @return an {@code IntSummaryStatistics} describing various summary data 536 * about the elements of this stream 537 */ 538 IntSummaryStatistics summaryStatistics(); 539 540 /** 541 * Returns whether any elements of this stream match the provided 542 * predicate. May not evaluate the predicate on all elements if not 543 * necessary for determining the result. 544 * 545 * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting 546 * terminal operation</a>. 547 * 548 * @param predicate a <a href="package-summary.html#NonInterference">non-interfering, 549 * stateless</a> predicate to apply to elements of this 550 * stream 551 * @return {@code true} if any elements of the stream match the provided 552 * predicate otherwise {@code false} 553 */ 554 boolean anyMatch(IntPredicate predicate); 555 556 /** 557 * Returns whether all elements of this stream match the provided predicate. 558 * May not evaluate the predicate on all elements if not necessary for 559 * determining the result. 560 * 561 * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting 562 * terminal operation</a>. 563 * 564 * @param predicate a <a href="package-summary.html#NonInterference">non-interfering, 565 * stateless</a> predicate to apply to elements of this 566 * stream 567 * @return {@code true} if all elements of the stream match the provided 568 * predicate otherwise {@code false} 569 */ 570 boolean allMatch(IntPredicate predicate); 571 572 /** 573 * Returns whether no elements of this stream match the provided predicate. 574 * May not evaluate the predicate on all elements if not necessary for 575 * determining the result. 576 * 577 * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting 578 * terminal operation</a>. 579 * 580 * @param predicate a <a href="package-summary.html#NonInterference">non-interfering, 581 * stateless</a> predicate to apply to elements of this 582 * stream 583 * @return {@code true} if no elements of the stream match the provided 584 * predicate otherwise {@code false} 585 */ 586 boolean noneMatch(IntPredicate predicate); 587 588 /** 589 * Returns an {@link OptionalInt} describing the first element of this 590 * stream (in the encounter order), or an empty {@code OptionalInt} if the 591 * stream is empty. If the stream has no encounter order, then any element 592 * may be returned. 593 * 594 * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting 595 * terminal operation</a>. 596 * 597 * @return an {@code OptionalInt} describing the first element of this stream, 598 * or an empty {@code OptionalInt} if the stream is empty 599 */ 600 OptionalInt findFirst(); 601 602 /** 603 * Returns an {@link OptionalInt} describing some element of the stream, or 604 * an empty {@code OptionalInt} if the stream is empty. 605 * 606 * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting 607 * terminal operation</a>. 608 * 609 * <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 611 * 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 613 * in the encounter order is desired, use {@link #findFirst()} instead.) 614 * 615 * @return an {@code OptionalInt} describing some element of this stream, or 616 * an empty {@code OptionalInt} if the stream is empty 617 * @see #findFirst() 618 */ 619 OptionalInt findAny(); 620 621 /** 622 * Returns a {@code LongStream} consisting of the elements of this stream, 623 * converted to {@code long}. 624 * 625 * @return a {@code LongStream} consisting of the elements of this stream, 626 * converted to {@code long} 627 */ 628 LongStream asLongStream(); 629 630 /** 631 * Returns a {@code DoubleStream} consisting of the elements of this stream, 632 * converted to {@code double}. 633 * 634 * @return a {@code DoubleStream} consisting of the elements of this stream, 635 * converted to {@code double} 636 */ 637 DoubleStream asDoubleStream(); 638 639 /** 640 * Returns a {@code Stream} consisting of the elements of this stream, 641 * each boxed to an {@code Integer}. 642 * 643 * @return a {@code Stream} consistent of the elements of this stream, 644 * each boxed to an {@code Integer} 645 */ 646 Stream<Integer> boxed(); 647 648 @Override 649 IntStream sequential(); 650 651 @Override 652 IntStream parallel(); 653 654 @Override 655 PrimitiveIterator.OfInt iterator(); 656 657 @Override 658 Spliterator.OfInt spliterator(); 659 660 // Static factories 661 662 /** 663 * Returns a builder for an {@code IntStream}. 664 * 665 * @return a stream builder 666 */ 667 public static StreamBuilder.OfInt builder() { 668 return new Streams.IntStreamBuilderImpl(); 669 } 670 671 /** 672 * Returns an empty sequential {@code IntStream}. 673 * 674 * @return an empty sequential stream 675 */ 676 public static IntStream empty() { 677 return StreamSupport.intStream(Spliterators.emptyIntSpliterator()); 678 } 679 680 /** 681 * Returns a sequential {@code IntStream} containing a single element. 682 * 683 * @param t the single element 684 * @return a singleton sequential stream 685 */ 686 public static IntStream of(int t) { 687 return StreamSupport.intStream(new Streams.IntStreamBuilderImpl(t)); 688 } 689 690 /** 691 * Returns a sequential stream whose elements are the specified values. 692 * 693 * @param values the elements of the new stream 694 * @return the new stream 695 */ 696 public static IntStream of(int... values) { 697 return Arrays.stream(values); 698 } 699 700 /** 701 * Returns an infinite sequential {@code IntStream} produced by iterative 702 * application of a function {@code f} to an initial element {@code seed}, 703 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)}, 704 * {@code f(f(seed))}, etc. 705 * 706 * <p>The first element (position {@code 0}) in the {@code IntStream} will be 707 * the provided {@code seed}. For {@code n > 0}, the element at position 708 * {@code n}, will be the result of applying the function {@code f} to the 709 * element at position {@code n - 1}. 710 * 711 * @param seed the initial element 712 * @param f a function to be applied to to the previous element to produce 713 * a new element 714 * @return A new sequential {@code IntStream} 715 */ 716 public static IntStream iterate(final int seed, final IntUnaryOperator f) { 717 Objects.requireNonNull(f); 718 final PrimitiveIterator.OfInt iterator = new PrimitiveIterator.OfInt() { 719 int t = seed; 720 721 @Override 722 public boolean hasNext() { 723 return true; 724 } 725 726 @Override 727 public int nextInt() { 728 int v = t; 729 t = f.applyAsInt(t); 730 return v; 731 } 732 }; 733 return StreamSupport.intStream(Spliterators.spliteratorUnknownSize( 734 iterator, 735 Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL)); 736 } 737 738 /** 739 * Returns a sequential {@code IntStream} where each element is 740 * generated by an {@code IntSupplier}. This is suitable for generating 741 * constant streams, streams of random elements, etc. 742 * 743 * @param s the {@code IntSupplier} for generated elements 744 * @return a new sequential {@code IntStream} 745 */ 746 public static IntStream generate(IntSupplier s) { 747 Objects.requireNonNull(s); 748 return StreamSupport.intStream( 749 new StreamSpliterators.InfiniteSupplyingSpliterator.OfInt(Long.MAX_VALUE, s)); 750 } 751 752 /** 753 * Returns a sequential {@code IntStream} from {@code startInclusive} 754 * (inclusive) to {@code endExclusive} (exclusive) by an incremental step of 755 * {@code 1}. 756 * 757 * @apiNote 758 * <p>An equivalent sequence of increasing values can be produced 759 * sequentially using a {@code for} loop as follows: 760 * <pre>{@code 761 * for (int i = startInclusive; i < endExclusive ; i++) { ... } 762 * }</pre> 763 * 764 * @param startInclusive the (inclusive) initial value 765 * @param endExclusive the exclusive upper bound 766 * @return a sequential {@code IntStream} for the range of {@code int} 767 * elements 768 */ 769 public static IntStream range(int startInclusive, int endExclusive) { 770 if (startInclusive >= endExclusive) { 771 return empty(); 772 } else { 773 return StreamSupport.intStream( 774 new Streams.RangeIntSpliterator(startInclusive, endExclusive, false)); 775 } 776 } 777 778 /** 779 * Returns a sequential {@code IntStream} from {@code startInclusive} 780 * (inclusive) to {@code endInclusive} (inclusive) by an incremental step of 781 * {@code 1}. 782 * 783 * @apiNote 784 * <p>An equivalent sequence of increasing values can be produced 785 * sequentially using a {@code for} loop as follows: 786 * <pre>{@code 787 * for (int i = startInclusive; i <= endInclusive ; i++) { ... } 788 * }</pre> 789 * 790 * @param startInclusive the (inclusive) initial value 791 * @param endInclusive the inclusive upper bound 792 * @return a sequential {@code IntStream} for the range of {@code int} 793 * elements 794 */ 795 public static IntStream rangeClosed(int startInclusive, int endInclusive) { 796 if (startInclusive > endInclusive) { 797 return empty(); 798 } else { 799 return StreamSupport.intStream( 800 new Streams.RangeIntSpliterator(startInclusive, endInclusive, true)); 801 } 802 } 803 804 /** 805 * Creates a lazy concatenated {@code IntStream} whose elements are all the 806 * elements of a first {@code IntStream} succeeded by all the elements of the 807 * second {@code IntStream}. The resulting stream is ordered if both 808 * of the input streams are ordered, and parallel if either of the input 809 * streams is parallel. 810 * 811 * @param a the first stream 812 * @param b the second stream to concatenate on to end of the first stream 813 * @return the concatenation of the two streams 814 */ 815 public static IntStream concat(IntStream a, IntStream b) { 816 Objects.requireNonNull(a); 817 Objects.requireNonNull(b); 818 819 Spliterator.OfInt split = new Streams.ConcatSpliterator.OfInt( 820 a.spliterator(), b.spliterator()); 821 return (a.isParallel() || b.isParallel()) 822 ? StreamSupport.intParallelStream(split) 823 : StreamSupport.intStream(split); 824 } 825 }