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