author | psandoz |
Fri, 20 May 2016 11:47:39 +0200 | |
changeset 38442 | 387a4457880c |
parent 36223 | de2976b3614c |
child 38444 | a5cdecb7d181 |
permissions | -rw-r--r-- |
17167 | 1 |
/* |
36223
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
2 |
* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. |
17167 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
package java.util.stream; |
|
26 |
||
17195 | 27 |
import java.util.Arrays; |
17167 | 28 |
import java.util.DoubleSummaryStatistics; |
17195 | 29 |
import java.util.Objects; |
17167 | 30 |
import java.util.OptionalDouble; |
31 |
import java.util.PrimitiveIterator; |
|
32 |
import java.util.Spliterator; |
|
17195 | 33 |
import java.util.Spliterators; |
17167 | 34 |
import java.util.function.BiConsumer; |
35 |
import java.util.function.DoubleBinaryOperator; |
|
36 |
import java.util.function.DoubleConsumer; |
|
37 |
import java.util.function.DoubleFunction; |
|
38 |
import java.util.function.DoublePredicate; |
|
17195 | 39 |
import java.util.function.DoubleSupplier; |
17167 | 40 |
import java.util.function.DoubleToIntFunction; |
41 |
import java.util.function.DoubleToLongFunction; |
|
42 |
import java.util.function.DoubleUnaryOperator; |
|
43 |
import java.util.function.Function; |
|
44 |
import java.util.function.ObjDoubleConsumer; |
|
45 |
import java.util.function.Supplier; |
|
46 |
||
47 |
/** |
|
21339 | 48 |
* A sequence of primitive double-valued elements supporting sequential and parallel |
49 |
* aggregate operations. This is the {@code double} primitive specialization of |
|
50 |
* {@link Stream}. |
|
51 |
* |
|
52 |
* <p>The following example illustrates an aggregate operation using |
|
53 |
* {@link Stream} and {@link DoubleStream}, computing the sum of the weights of the |
|
54 |
* red widgets: |
|
19850 | 55 |
* |
56 |
* <pre>{@code |
|
57 |
* double sum = widgets.stream() |
|
58 |
* .filter(w -> w.getColor() == RED) |
|
59 |
* .mapToDouble(w -> w.getWeight()) |
|
60 |
* .sum(); |
|
61 |
* }</pre> |
|
62 |
* |
|
21339 | 63 |
* See the class documentation for {@link Stream} and the package documentation |
64 |
* for <a href="package-summary.html">java.util.stream</a> for additional |
|
65 |
* specification of streams, stream operations, stream pipelines, and |
|
66 |
* parallelism. |
|
17167 | 67 |
* |
68 |
* @since 1.8 |
|
21339 | 69 |
* @see Stream |
17167 | 70 |
* @see <a href="package-summary.html">java.util.stream</a> |
71 |
*/ |
|
72 |
public interface DoubleStream extends BaseStream<Double, DoubleStream> { |
|
73 |
||
74 |
/** |
|
75 |
* Returns a stream consisting of the elements of this stream that match |
|
76 |
* the given predicate. |
|
77 |
* |
|
78 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
79 |
* operation</a>. |
|
80 |
* |
|
21339 | 81 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>, |
82 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
83 |
* predicate to apply to each element to determine if it |
|
84 |
* should be included |
|
17167 | 85 |
* @return the new stream |
86 |
*/ |
|
87 |
DoubleStream filter(DoublePredicate predicate); |
|
88 |
||
89 |
/** |
|
90 |
* Returns a stream consisting of the results of applying the given |
|
91 |
* function to the elements of this stream. |
|
92 |
* |
|
93 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
94 |
* operation</a>. |
|
95 |
* |
|
21339 | 96 |
* @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>, |
97 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
98 |
* function to apply to each element |
|
17167 | 99 |
* @return the new stream |
100 |
*/ |
|
101 |
DoubleStream map(DoubleUnaryOperator mapper); |
|
102 |
||
103 |
/** |
|
104 |
* Returns an object-valued {@code Stream} consisting of the results of |
|
105 |
* applying the given function to the elements of this stream. |
|
106 |
* |
|
107 |
* <p>This is an <a href="package-summary.html#StreamOps"> |
|
108 |
* intermediate operation</a>. |
|
109 |
* |
|
110 |
* @param <U> the element type of the new stream |
|
21339 | 111 |
* @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>, |
112 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
113 |
* function to apply to each element |
|
17167 | 114 |
* @return the new stream |
115 |
*/ |
|
116 |
<U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper); |
|
117 |
||
118 |
/** |
|
119 |
* Returns an {@code IntStream} consisting of the results of applying the |
|
120 |
* given function to the elements of this stream. |
|
121 |
* |
|
122 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
123 |
* operation</a>. |
|
124 |
* |
|
21339 | 125 |
* @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>, |
126 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
127 |
* function to apply to each element |
|
17167 | 128 |
* @return the new stream |
129 |
*/ |
|
130 |
IntStream mapToInt(DoubleToIntFunction mapper); |
|
131 |
||
132 |
/** |
|
133 |
* Returns a {@code LongStream} consisting of the results of applying the |
|
134 |
* given function to the elements of this stream. |
|
135 |
* |
|
136 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
137 |
* operation</a>. |
|
138 |
* |
|
21339 | 139 |
* @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>, |
140 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
141 |
* function to apply to each element |
|
17167 | 142 |
* @return the new stream |
143 |
*/ |
|
144 |
LongStream mapToLong(DoubleToLongFunction mapper); |
|
145 |
||
146 |
/** |
|
147 |
* Returns a stream consisting of the results of replacing each element of |
|
22352
ecbf37860ffa
8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents:
21846
diff
changeset
|
148 |
* this stream with the contents of a mapped stream produced by applying |
ecbf37860ffa
8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents:
21846
diff
changeset
|
149 |
* the provided mapping function to each element. Each mapped stream is |
ecbf37860ffa
8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents:
21846
diff
changeset
|
150 |
* {@link java.util.stream.BaseStream#close() closed} after its contents |
ecbf37860ffa
8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents:
21846
diff
changeset
|
151 |
* have been placed into this stream. (If a mapped stream is {@code null} |
ecbf37860ffa
8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents:
21846
diff
changeset
|
152 |
* an empty stream is used, instead.) |
17167 | 153 |
* |
154 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
155 |
* operation</a>. |
|
156 |
* |
|
21339 | 157 |
* @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>, |
158 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
159 |
* function to apply to each element which produces a |
|
160 |
* {@code DoubleStream} of new values |
|
17167 | 161 |
* @return the new stream |
162 |
* @see Stream#flatMap(Function) |
|
163 |
*/ |
|
164 |
DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper); |
|
165 |
||
166 |
/** |
|
167 |
* Returns a stream consisting of the distinct elements of this stream. The |
|
168 |
* elements are compared for equality according to |
|
169 |
* {@link java.lang.Double#compare(double, double)}. |
|
170 |
* |
|
171 |
* <p>This is a <a href="package-summary.html#StreamOps">stateful |
|
172 |
* intermediate operation</a>. |
|
173 |
* |
|
174 |
* @return the result stream |
|
175 |
*/ |
|
176 |
DoubleStream distinct(); |
|
177 |
||
178 |
/** |
|
179 |
* Returns a stream consisting of the elements of this stream in sorted |
|
180 |
* order. The elements are compared for equality according to |
|
181 |
* {@link java.lang.Double#compare(double, double)}. |
|
182 |
* |
|
183 |
* <p>This is a <a href="package-summary.html#StreamOps">stateful |
|
184 |
* intermediate operation</a>. |
|
185 |
* |
|
186 |
* @return the result stream |
|
187 |
*/ |
|
188 |
DoubleStream sorted(); |
|
189 |
||
190 |
/** |
|
191 |
* Returns a stream consisting of the elements of this stream, additionally |
|
192 |
* performing the provided action on each element as elements are consumed |
|
193 |
* from the resulting stream. |
|
194 |
* |
|
195 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
196 |
* operation</a>. |
|
197 |
* |
|
198 |
* <p>For parallel stream pipelines, the action may be called at |
|
199 |
* whatever time and in whatever thread the element is made available by the |
|
200 |
* upstream operation. If the action modifies shared state, |
|
201 |
* it is responsible for providing the required synchronization. |
|
202 |
* |
|
203 |
* @apiNote This method exists mainly to support debugging, where you want |
|
204 |
* to see the elements as they flow past a certain point in a pipeline: |
|
205 |
* <pre>{@code |
|
21846
c10feb34bc0b
8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents:
21339
diff
changeset
|
206 |
* DoubleStream.of(1, 2, 3, 4) |
c10feb34bc0b
8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents:
21339
diff
changeset
|
207 |
* .filter(e -> e > 2) |
c10feb34bc0b
8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents:
21339
diff
changeset
|
208 |
* .peek(e -> System.out.println("Filtered value: " + e)) |
c10feb34bc0b
8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents:
21339
diff
changeset
|
209 |
* .map(e -> e * e) |
c10feb34bc0b
8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents:
21339
diff
changeset
|
210 |
* .peek(e -> System.out.println("Mapped value: " + e)) |
c10feb34bc0b
8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents:
21339
diff
changeset
|
211 |
* .sum(); |
17167 | 212 |
* }</pre> |
213 |
* |
|
38442
387a4457880c
8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents:
36223
diff
changeset
|
214 |
* <p>In cases where stream implementation is able to optimize away the |
387a4457880c
8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents:
36223
diff
changeset
|
215 |
* production of some or all the elements (such as with short-circuiting |
387a4457880c
8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents:
36223
diff
changeset
|
216 |
* operations like {@code findFirst}, or in the example described in |
387a4457880c
8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents:
36223
diff
changeset
|
217 |
* {@link #count}), the action will not be invoked for those elements. |
387a4457880c
8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents:
36223
diff
changeset
|
218 |
* |
19850 | 219 |
* @param action a <a href="package-summary.html#NonInterference"> |
21339 | 220 |
* non-interfering</a> action to perform on the elements as |
221 |
* they are consumed from the stream |
|
17167 | 222 |
* @return the new stream |
223 |
*/ |
|
19850 | 224 |
DoubleStream peek(DoubleConsumer action); |
17167 | 225 |
|
226 |
/** |
|
227 |
* Returns a stream consisting of the elements of this stream, truncated |
|
228 |
* to be no longer than {@code maxSize} in length. |
|
229 |
* |
|
230 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
231 |
* stateful intermediate operation</a>. |
|
232 |
* |
|
20866
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
233 |
* @apiNote |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
234 |
* While {@code limit()} is generally a cheap operation on sequential |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
235 |
* stream pipelines, it can be quite expensive on ordered parallel pipelines, |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
236 |
* especially for large values of {@code maxSize}, since {@code limit(n)} |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
237 |
* is constrained to return not just any <em>n</em> elements, but the |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
238 |
* <em>first n</em> elements in the encounter order. Using an unordered |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
239 |
* stream source (such as {@link #generate(DoubleSupplier)}) or removing the |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
240 |
* ordering constraint with {@link #unordered()} may result in significant |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
241 |
* speedups of {@code limit()} in parallel pipelines, if the semantics of |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
242 |
* your situation permit. If consistency with encounter order is required, |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
243 |
* and you are experiencing poor performance or memory utilization with |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
244 |
* {@code limit()} in parallel pipelines, switching to sequential execution |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
245 |
* with {@link #sequential()} may improve performance. |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
246 |
* |
17167 | 247 |
* @param maxSize the number of elements the stream should be limited to |
248 |
* @return the new stream |
|
249 |
* @throws IllegalArgumentException if {@code maxSize} is negative |
|
250 |
*/ |
|
251 |
DoubleStream limit(long maxSize); |
|
252 |
||
253 |
/** |
|
254 |
* Returns a stream consisting of the remaining elements of this stream |
|
20866
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
255 |
* after discarding the first {@code n} elements of the stream. |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
256 |
* If this stream contains fewer than {@code n} elements then an |
17167 | 257 |
* empty stream will be returned. |
258 |
* |
|
259 |
* <p>This is a <a href="package-summary.html#StreamOps">stateful |
|
260 |
* intermediate operation</a>. |
|
261 |
* |
|
20866
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
262 |
* @apiNote |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
263 |
* While {@code skip()} is generally a cheap operation on sequential |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
264 |
* stream pipelines, it can be quite expensive on ordered parallel pipelines, |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
265 |
* especially for large values of {@code n}, since {@code skip(n)} |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
266 |
* is constrained to skip not just any <em>n</em> elements, but the |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
267 |
* <em>first n</em> elements in the encounter order. Using an unordered |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
268 |
* stream source (such as {@link #generate(DoubleSupplier)}) or removing the |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
269 |
* ordering constraint with {@link #unordered()} may result in significant |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
270 |
* speedups of {@code skip()} in parallel pipelines, if the semantics of |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
271 |
* your situation permit. If consistency with encounter order is required, |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
272 |
* and you are experiencing poor performance or memory utilization with |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
273 |
* {@code skip()} in parallel pipelines, switching to sequential execution |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
274 |
* with {@link #sequential()} may improve performance. |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
275 |
* |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
276 |
* @param n the number of leading elements to skip |
17167 | 277 |
* @return the new stream |
20866
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
278 |
* @throws IllegalArgumentException if {@code n} is negative |
17167 | 279 |
*/ |
20866
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
20758
diff
changeset
|
280 |
DoubleStream skip(long n); |
17167 | 281 |
|
282 |
/** |
|
31644
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
283 |
* Returns, if this stream is ordered, a stream consisting of the longest |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
284 |
* prefix of elements taken from this stream that match the given predicate. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
285 |
* Otherwise returns, if this stream is unordered, a stream consisting of a |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
286 |
* subset of elements taken from this stream that match the given predicate. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
287 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
288 |
* <p>If this stream is ordered then the longest prefix is a contiguous |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
289 |
* sequence of elements of this stream that match the given predicate. The |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
290 |
* first element of the sequence is the first element of this stream, and |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
291 |
* the element immediately following the last element of the sequence does |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
292 |
* not match the given predicate. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
293 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
294 |
* <p>If this stream is unordered, and some (but not all) elements of this |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
295 |
* stream match the given predicate, then the behavior of this operation is |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
296 |
* nondeterministic; it is free to take any subset of matching elements |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
297 |
* (which includes the empty set). |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
298 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
299 |
* <p>Independent of whether this stream is ordered or unordered if all |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
300 |
* elements of this stream match the given predicate then this operation |
32002
e378c0dc767e
8130828: Fix some typos and omissions in the the j.u.stream JavaDoc
psandoz
parents:
31644
diff
changeset
|
301 |
* takes all elements (the result is the same as the input), or if no |
31644
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
302 |
* elements of the stream match the given predicate then no elements are |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
303 |
* taken (the result is an empty stream). |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
304 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
305 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
306 |
* stateful intermediate operation</a>. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
307 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
308 |
* @implSpec |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
309 |
* The default implementation obtains the {@link #spliterator() spliterator} |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
310 |
* of this stream, wraps that spliterator so as to support the semantics |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
311 |
* of this operation on traversal, and returns a new stream associated with |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
312 |
* the wrapped spliterator. The returned stream preserves the execution |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
313 |
* characteristics of this stream (namely parallel or sequential execution |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
314 |
* as per {@link #isParallel()}) but the wrapped spliterator may choose to |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
315 |
* not support splitting. When the returned stream is closed, the close |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
316 |
* handlers for both the returned and this stream are invoked. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
317 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
318 |
* @apiNote |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
319 |
* While {@code takeWhile()} is generally a cheap operation on sequential |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
320 |
* stream pipelines, it can be quite expensive on ordered parallel |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
321 |
* pipelines, since the operation is constrained to return not just any |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
322 |
* valid prefix, but the longest prefix of elements in the encounter order. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
323 |
* Using an unordered stream source (such as |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
324 |
* {@link #generate(DoubleSupplier)}) or removing the ordering constraint |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
325 |
* with {@link #unordered()} may result in significant speedups of |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
326 |
* {@code takeWhile()} in parallel pipelines, if the semantics of your |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
327 |
* situation permit. If consistency with encounter order is required, and |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
328 |
* you are experiencing poor performance or memory utilization with |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
329 |
* {@code takeWhile()} in parallel pipelines, switching to sequential |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
330 |
* execution with {@link #sequential()} may improve performance. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
331 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
332 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>, |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
333 |
* <a href="package-summary.html#Statelessness">stateless</a> |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
334 |
* predicate to apply to elements to determine the longest |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
335 |
* prefix of elements. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
336 |
* @return the new stream |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
32002
diff
changeset
|
337 |
* @since 9 |
31644
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
338 |
*/ |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
339 |
default DoubleStream takeWhile(DoublePredicate predicate) { |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
340 |
Objects.requireNonNull(predicate); |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
341 |
// Reuses the unordered spliterator, which, when encounter is present, |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
342 |
// is safe to use as long as it configured not to split |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
343 |
return StreamSupport.doubleStream( |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
344 |
new WhileOps.UnorderedWhileSpliterator.OfDouble.Taking(spliterator(), true, predicate), |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
345 |
isParallel()).onClose(this::close); |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
346 |
} |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
347 |
|
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
348 |
/** |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
349 |
* Returns, if this stream is ordered, a stream consisting of the remaining |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
350 |
* elements of this stream after dropping the longest prefix of elements |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
351 |
* that match the given predicate. Otherwise returns, if this stream is |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
352 |
* unordered, a stream consisting of the remaining elements of this stream |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
353 |
* after dropping a subset of elements that match the given predicate. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
354 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
355 |
* <p>If this stream is ordered then the longest prefix is a contiguous |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
356 |
* sequence of elements of this stream that match the given predicate. The |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
357 |
* first element of the sequence is the first element of this stream, and |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
358 |
* the element immediately following the last element of the sequence does |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
359 |
* not match the given predicate. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
360 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
361 |
* <p>If this stream is unordered, and some (but not all) elements of this |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
362 |
* stream match the given predicate, then the behavior of this operation is |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
363 |
* nondeterministic; it is free to drop any subset of matching elements |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
364 |
* (which includes the empty set). |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
365 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
366 |
* <p>Independent of whether this stream is ordered or unordered if all |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
367 |
* elements of this stream match the given predicate then this operation |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
368 |
* drops all elements (the result is an empty stream), or if no elements of |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
369 |
* the stream match the given predicate then no elements are dropped (the |
32002
e378c0dc767e
8130828: Fix some typos and omissions in the the j.u.stream JavaDoc
psandoz
parents:
31644
diff
changeset
|
370 |
* result is the same as the input). |
31644
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
371 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
372 |
* <p>This is a <a href="package-summary.html#StreamOps">stateful |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
373 |
* intermediate operation</a>. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
374 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
375 |
* @implSpec |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
376 |
* The default implementation obtains the {@link #spliterator() spliterator} |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
377 |
* of this stream, wraps that spliterator so as to support the semantics |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
378 |
* of this operation on traversal, and returns a new stream associated with |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
379 |
* the wrapped spliterator. The returned stream preserves the execution |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
380 |
* characteristics of this stream (namely parallel or sequential execution |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
381 |
* as per {@link #isParallel()}) but the wrapped spliterator may choose to |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
382 |
* not support splitting. When the returned stream is closed, the close |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
383 |
* handlers for both the returned and this stream are invoked. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
384 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
385 |
* @apiNote |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
386 |
* While {@code dropWhile()} is generally a cheap operation on sequential |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
387 |
* stream pipelines, it can be quite expensive on ordered parallel |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
388 |
* pipelines, since the operation is constrained to return not just any |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
389 |
* valid prefix, but the longest prefix of elements in the encounter order. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
390 |
* Using an unordered stream source (such as |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
391 |
* {@link #generate(DoubleSupplier)}) or removing the ordering constraint |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
392 |
* with {@link #unordered()} may result in significant speedups of |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
393 |
* {@code dropWhile()} in parallel pipelines, if the semantics of your |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
394 |
* situation permit. If consistency with encounter order is required, and |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
395 |
* you are experiencing poor performance or memory utilization with |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
396 |
* {@code dropWhile()} in parallel pipelines, switching to sequential |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
397 |
* execution with {@link #sequential()} may improve performance. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
398 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
399 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>, |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
400 |
* <a href="package-summary.html#Statelessness">stateless</a> |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
401 |
* predicate to apply to elements to determine the longest |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
402 |
* prefix of elements. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
403 |
* @return the new stream |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
32002
diff
changeset
|
404 |
* @since 9 |
31644
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
405 |
*/ |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
406 |
default DoubleStream dropWhile(DoublePredicate predicate) { |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
407 |
Objects.requireNonNull(predicate); |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
408 |
// Reuses the unordered spliterator, which, when encounter is present, |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
409 |
// is safe to use as long as it configured not to split |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
410 |
return StreamSupport.doubleStream( |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
411 |
new WhileOps.UnorderedWhileSpliterator.OfDouble.Dropping(spliterator(), true, predicate), |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
412 |
isParallel()).onClose(this::close); |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
413 |
} |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
414 |
|
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
415 |
/** |
17167 | 416 |
* Performs an action for each element of this stream. |
417 |
* |
|
418 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
419 |
* operation</a>. |
|
420 |
* |
|
421 |
* <p>For parallel stream pipelines, this operation does <em>not</em> |
|
422 |
* guarantee to respect the encounter order of the stream, as doing so |
|
423 |
* would sacrifice the benefit of parallelism. For any given element, the |
|
424 |
* action may be performed at whatever time and in whatever thread the |
|
425 |
* library chooses. If the action accesses shared state, it is |
|
426 |
* responsible for providing the required synchronization. |
|
427 |
* |
|
428 |
* @param action a <a href="package-summary.html#NonInterference"> |
|
429 |
* non-interfering</a> action to perform on the elements |
|
430 |
*/ |
|
431 |
void forEach(DoubleConsumer action); |
|
432 |
||
433 |
/** |
|
434 |
* Performs an action for each element of this stream, guaranteeing that |
|
435 |
* each element is processed in encounter order for streams that have a |
|
436 |
* defined encounter order. |
|
437 |
* |
|
438 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
439 |
* operation</a>. |
|
440 |
* |
|
441 |
* @param action a <a href="package-summary.html#NonInterference"> |
|
442 |
* non-interfering</a> action to perform on the elements |
|
443 |
* @see #forEach(DoubleConsumer) |
|
444 |
*/ |
|
445 |
void forEachOrdered(DoubleConsumer action); |
|
446 |
||
447 |
/** |
|
448 |
* Returns an array containing the elements of this stream. |
|
449 |
* |
|
450 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
451 |
* operation</a>. |
|
452 |
* |
|
453 |
* @return an array containing the elements of this stream |
|
454 |
*/ |
|
455 |
double[] toArray(); |
|
456 |
||
457 |
/** |
|
458 |
* Performs a <a href="package-summary.html#Reduction">reduction</a> on the |
|
459 |
* elements of this stream, using the provided identity value and an |
|
460 |
* <a href="package-summary.html#Associativity">associative</a> |
|
461 |
* accumulation function, and returns the reduced value. This is equivalent |
|
462 |
* to: |
|
463 |
* <pre>{@code |
|
464 |
* double result = identity; |
|
465 |
* for (double element : this stream) |
|
22996
0ce70f4ef909
8029944: Primitive Stream reduce method documentation pseudo code misidentifies apply method
michaelm
parents:
22352
diff
changeset
|
466 |
* result = accumulator.applyAsDouble(result, element) |
17167 | 467 |
* return result; |
468 |
* }</pre> |
|
469 |
* |
|
470 |
* but is not constrained to execute sequentially. |
|
471 |
* |
|
472 |
* <p>The {@code identity} value must be an identity for the accumulator |
|
473 |
* function. This means that for all {@code x}, |
|
474 |
* {@code accumulator.apply(identity, x)} is equal to {@code x}. |
|
475 |
* The {@code accumulator} function must be an |
|
476 |
* <a href="package-summary.html#Associativity">associative</a> function. |
|
477 |
* |
|
478 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
479 |
* operation</a>. |
|
480 |
* |
|
481 |
* @apiNote Sum, min, max, and average are all special cases of reduction. |
|
482 |
* Summing a stream of numbers can be expressed as: |
|
483 |
||
484 |
* <pre>{@code |
|
485 |
* double sum = numbers.reduce(0, (a, b) -> a+b); |
|
486 |
* }</pre> |
|
487 |
* |
|
488 |
* or more compactly: |
|
489 |
* |
|
490 |
* <pre>{@code |
|
491 |
* double sum = numbers.reduce(0, Double::sum); |
|
492 |
* }</pre> |
|
493 |
* |
|
494 |
* <p>While this may seem a more roundabout way to perform an aggregation |
|
495 |
* compared to simply mutating a running total in a loop, reduction |
|
496 |
* operations parallelize more gracefully, without needing additional |
|
497 |
* synchronization and with greatly reduced risk of data races. |
|
498 |
* |
|
499 |
* @param identity the identity value for the accumulating function |
|
21339 | 500 |
* @param op an <a href="package-summary.html#Associativity">associative</a>, |
501 |
* <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
502 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
503 |
* function for combining two values |
|
17167 | 504 |
* @return the result of the reduction |
505 |
* @see #sum() |
|
506 |
* @see #min() |
|
507 |
* @see #max() |
|
508 |
* @see #average() |
|
509 |
*/ |
|
510 |
double reduce(double identity, DoubleBinaryOperator op); |
|
511 |
||
512 |
/** |
|
513 |
* Performs a <a href="package-summary.html#Reduction">reduction</a> on the |
|
514 |
* elements of this stream, using an |
|
515 |
* <a href="package-summary.html#Associativity">associative</a> accumulation |
|
516 |
* function, and returns an {@code OptionalDouble} describing the reduced |
|
517 |
* value, if any. This is equivalent to: |
|
518 |
* <pre>{@code |
|
519 |
* boolean foundAny = false; |
|
520 |
* double result = null; |
|
521 |
* for (double element : this stream) { |
|
522 |
* if (!foundAny) { |
|
523 |
* foundAny = true; |
|
524 |
* result = element; |
|
525 |
* } |
|
526 |
* else |
|
22996
0ce70f4ef909
8029944: Primitive Stream reduce method documentation pseudo code misidentifies apply method
michaelm
parents:
22352
diff
changeset
|
527 |
* result = accumulator.applyAsDouble(result, element); |
17167 | 528 |
* } |
529 |
* return foundAny ? OptionalDouble.of(result) : OptionalDouble.empty(); |
|
530 |
* }</pre> |
|
531 |
* |
|
532 |
* but is not constrained to execute sequentially. |
|
533 |
* |
|
534 |
* <p>The {@code accumulator} function must be an |
|
535 |
* <a href="package-summary.html#Associativity">associative</a> function. |
|
536 |
* |
|
537 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
538 |
* operation</a>. |
|
539 |
* |
|
21339 | 540 |
* @param op an <a href="package-summary.html#Associativity">associative</a>, |
541 |
* <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
542 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
543 |
* function for combining two values |
|
17167 | 544 |
* @return the result of the reduction |
545 |
* @see #reduce(double, DoubleBinaryOperator) |
|
546 |
*/ |
|
547 |
OptionalDouble reduce(DoubleBinaryOperator op); |
|
548 |
||
549 |
/** |
|
550 |
* Performs a <a href="package-summary.html#MutableReduction">mutable |
|
551 |
* reduction</a> operation on the elements of this stream. A mutable |
|
19850 | 552 |
* reduction is one in which the reduced value is a mutable result container, |
17167 | 553 |
* such as an {@code ArrayList}, and elements are incorporated by updating |
19850 | 554 |
* the state of the result rather than by replacing the result. This |
17167 | 555 |
* produces a result equivalent to: |
556 |
* <pre>{@code |
|
19850 | 557 |
* R result = supplier.get(); |
17167 | 558 |
* for (double element : this stream) |
559 |
* accumulator.accept(result, element); |
|
560 |
* return result; |
|
561 |
* }</pre> |
|
562 |
* |
|
563 |
* <p>Like {@link #reduce(double, DoubleBinaryOperator)}, {@code collect} |
|
564 |
* operations can be parallelized without requiring additional |
|
565 |
* synchronization. |
|
566 |
* |
|
567 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
568 |
* operation</a>. |
|
569 |
* |
|
570 |
* @param <R> type of the result |
|
19850 | 571 |
* @param supplier a function that creates a new result container. For a |
572 |
* parallel execution, this function may be called |
|
573 |
* multiple times and must return a fresh value each time. |
|
21339 | 574 |
* @param accumulator an <a href="package-summary.html#Associativity">associative</a>, |
575 |
* <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
576 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
577 |
* function for incorporating an additional element into a result |
|
578 |
* @param combiner an <a href="package-summary.html#Associativity">associative</a>, |
|
579 |
* <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
580 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
581 |
* function for combining two values, which must be |
|
582 |
* compatible with the accumulator function |
|
17167 | 583 |
* @return the result of the reduction |
584 |
* @see Stream#collect(Supplier, BiConsumer, BiConsumer) |
|
585 |
*/ |
|
19850 | 586 |
<R> R collect(Supplier<R> supplier, |
17167 | 587 |
ObjDoubleConsumer<R> accumulator, |
588 |
BiConsumer<R, R> combiner); |
|
589 |
||
590 |
/** |
|
20758
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
591 |
* Returns the sum of elements in this stream. |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
592 |
* |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
593 |
* Summation is a special case of a <a |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
594 |
* href="package-summary.html#Reduction">reduction</a>. If |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
595 |
* floating-point summation were exact, this method would be |
17167 | 596 |
* equivalent to: |
20758
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
597 |
* |
17167 | 598 |
* <pre>{@code |
599 |
* return reduce(0, Double::sum); |
|
600 |
* }</pre> |
|
601 |
* |
|
20758
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
602 |
* However, since floating-point summation is not exact, the above |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
603 |
* code is not necessarily equivalent to the summation computation |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
604 |
* done by this method. |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
605 |
* |
25753
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
606 |
* <p>The value of a floating-point sum is a function both |
20758
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
607 |
* of the input values as well as the order of addition |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
608 |
* operations. The order of addition operations of this method is |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
609 |
* intentionally not defined to allow for implementation |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
610 |
* flexibility to improve the speed and accuracy of the computed |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
611 |
* result. |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
612 |
* |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
613 |
* In particular, this method may be implemented using compensated |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
614 |
* summation or other technique to reduce the error bound in the |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
615 |
* numerical sum compared to a simple summation of {@code double} |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
616 |
* values. |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
617 |
* |
25753
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
618 |
* Because of the unspecified order of operations and the |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
619 |
* possibility of using differing summation schemes, the output of |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
620 |
* this method may vary on the same input elements. |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
621 |
* |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
622 |
* <p>Various conditions can result in a non-finite sum being |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
623 |
* computed. This can occur even if the all the elements |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
624 |
* being summed are finite. If any element is non-finite, |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
625 |
* the sum will be non-finite: |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
626 |
* |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
627 |
* <ul> |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
628 |
* |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
629 |
* <li>If any element is a NaN, then the final sum will be |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
630 |
* NaN. |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
631 |
* |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
632 |
* <li>If the elements contain one or more infinities, the |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
633 |
* sum will be infinite or NaN. |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
634 |
* |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
635 |
* <ul> |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
636 |
* |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
637 |
* <li>If the elements contain infinities of opposite sign, |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
638 |
* the sum will be NaN. |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
639 |
* |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
640 |
* <li>If the elements contain infinities of one sign and |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
641 |
* an intermediate sum overflows to an infinity of the opposite |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
642 |
* sign, the sum may be NaN. |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
643 |
* |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
644 |
* </ul> |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
645 |
* |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
646 |
* </ul> |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
647 |
* |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
648 |
* It is possible for intermediate sums of finite values to |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
649 |
* overflow into opposite-signed infinities; if that occurs, the |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
650 |
* final sum will be NaN even if the elements are all |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
651 |
* finite. |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
652 |
* |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
653 |
* If all the elements are zero, the sign of zero is |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
654 |
* <em>not</em> guaranteed to be preserved in the final sum. |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
655 |
* |
19850 | 656 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
657 |
* operation</a>. |
|
658 |
* |
|
21339 | 659 |
* @apiNote Elements sorted by increasing absolute magnitude tend |
660 |
* to yield more accurate results. |
|
20758
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
661 |
* |
17167 | 662 |
* @return the sum of elements in this stream |
663 |
*/ |
|
664 |
double sum(); |
|
665 |
||
666 |
/** |
|
667 |
* Returns an {@code OptionalDouble} describing the minimum element of this |
|
668 |
* stream, or an empty OptionalDouble if this stream is empty. The minimum |
|
669 |
* element will be {@code Double.NaN} if any stream element was NaN. Unlike |
|
670 |
* the numerical comparison operators, this method considers negative zero |
|
671 |
* to be strictly smaller than positive zero. This is a special case of a |
|
19850 | 672 |
* <a href="package-summary.html#Reduction">reduction</a> and is |
17167 | 673 |
* equivalent to: |
674 |
* <pre>{@code |
|
675 |
* return reduce(Double::min); |
|
676 |
* }</pre> |
|
677 |
* |
|
19850 | 678 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
679 |
* operation</a>. |
|
680 |
* |
|
17167 | 681 |
* @return an {@code OptionalDouble} containing the minimum element of this |
682 |
* stream, or an empty optional if the stream is empty |
|
683 |
*/ |
|
684 |
OptionalDouble min(); |
|
685 |
||
686 |
/** |
|
687 |
* Returns an {@code OptionalDouble} describing the maximum element of this |
|
688 |
* stream, or an empty OptionalDouble if this stream is empty. The maximum |
|
689 |
* element will be {@code Double.NaN} if any stream element was NaN. Unlike |
|
690 |
* the numerical comparison operators, this method considers negative zero |
|
691 |
* to be strictly smaller than positive zero. This is a |
|
692 |
* special case of a |
|
19850 | 693 |
* <a href="package-summary.html#Reduction">reduction</a> and is |
17167 | 694 |
* equivalent to: |
695 |
* <pre>{@code |
|
696 |
* return reduce(Double::max); |
|
697 |
* }</pre> |
|
698 |
* |
|
19850 | 699 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
700 |
* operation</a>. |
|
701 |
* |
|
17167 | 702 |
* @return an {@code OptionalDouble} containing the maximum element of this |
703 |
* stream, or an empty optional if the stream is empty |
|
704 |
*/ |
|
705 |
OptionalDouble max(); |
|
706 |
||
707 |
/** |
|
708 |
* Returns the count of elements in this stream. This is a special case of |
|
19850 | 709 |
* a <a href="package-summary.html#Reduction">reduction</a> and is |
17167 | 710 |
* equivalent to: |
711 |
* <pre>{@code |
|
712 |
* return mapToLong(e -> 1L).sum(); |
|
713 |
* }</pre> |
|
714 |
* |
|
715 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>. |
|
716 |
* |
|
29489 | 717 |
* @apiNote |
718 |
* An implementation may choose to not execute the stream pipeline (either |
|
719 |
* sequentially or in parallel) if it is capable of computing the count |
|
720 |
* directly from the stream source. In such cases no source elements will |
|
721 |
* be traversed and no intermediate operations will be evaluated. |
|
722 |
* Behavioral parameters with side-effects, which are strongly discouraged |
|
723 |
* except for harmless cases such as debugging, may be affected. For |
|
724 |
* example, consider the following stream: |
|
725 |
* <pre>{@code |
|
726 |
* DoubleStream s = DoubleStream.of(1, 2, 3, 4); |
|
727 |
* long count = s.peek(System.out::println).count(); |
|
728 |
* }</pre> |
|
729 |
* The number of elements covered by the stream source is known and the |
|
730 |
* intermediate operation, {@code peek}, does not inject into or remove |
|
731 |
* elements from the stream (as may be the case for {@code flatMap} or |
|
732 |
* {@code filter} operations). Thus the count is 4 and there is no need to |
|
733 |
* execute the pipeline and, as a side-effect, print out the elements. |
|
734 |
* |
|
17167 | 735 |
* @return the count of elements in this stream |
736 |
*/ |
|
737 |
long count(); |
|
738 |
||
739 |
/** |
|
20758
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
740 |
* Returns an {@code OptionalDouble} describing the arithmetic |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
741 |
* mean of elements of this stream, or an empty optional if this |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
742 |
* stream is empty. |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
743 |
* |
25753
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
744 |
* <p>The computed average can vary numerically and have the |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
745 |
* special case behavior as computing the sum; see {@link #sum} |
b73bd85f9068
8030942: Explicitly state floating-point summation requirements on non-finite inputs
darcy
parents:
25526
diff
changeset
|
746 |
* for details. |
20758
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
747 |
* |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
748 |
* <p>The average is a special case of a <a |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
749 |
* href="package-summary.html#Reduction">reduction</a>. |
19850 | 750 |
* |
751 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
752 |
* operation</a>. |
|
17167 | 753 |
* |
20758
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
754 |
* @apiNote Elements sorted by increasing absolute magnitude tend |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
755 |
* to yield more accurate results. |
d8845d3fb428
8024354: Explicitly permit DoubleStream.sum()/average() implementations to use higher precision summation
darcy
parents:
19850
diff
changeset
|
756 |
* |
17167 | 757 |
* @return an {@code OptionalDouble} containing the average element of this |
758 |
* stream, or an empty optional if the stream is empty |
|
759 |
*/ |
|
760 |
OptionalDouble average(); |
|
761 |
||
762 |
/** |
|
763 |
* Returns a {@code DoubleSummaryStatistics} describing various summary data |
|
764 |
* about the elements of this stream. This is a special |
|
19850 | 765 |
* case of a <a href="package-summary.html#Reduction">reduction</a>. |
766 |
* |
|
767 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
768 |
* operation</a>. |
|
17167 | 769 |
* |
770 |
* @return a {@code DoubleSummaryStatistics} describing various summary data |
|
771 |
* about the elements of this stream |
|
772 |
*/ |
|
773 |
DoubleSummaryStatistics summaryStatistics(); |
|
774 |
||
775 |
/** |
|
776 |
* Returns whether any elements of this stream match the provided |
|
777 |
* predicate. May not evaluate the predicate on all elements if not |
|
21339 | 778 |
* necessary for determining the result. If the stream is empty then |
779 |
* {@code false} is returned and the predicate is not evaluated. |
|
17167 | 780 |
* |
781 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
782 |
* terminal operation</a>. |
|
783 |
* |
|
21339 | 784 |
* @apiNote |
785 |
* This method evaluates the <em>existential quantification</em> of the |
|
786 |
* predicate over the elements of the stream (for some x P(x)). |
|
787 |
* |
|
788 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
789 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
790 |
* predicate to apply to elements of this stream |
|
17167 | 791 |
* @return {@code true} if any elements of the stream match the provided |
21339 | 792 |
* predicate, otherwise {@code false} |
17167 | 793 |
*/ |
794 |
boolean anyMatch(DoublePredicate predicate); |
|
795 |
||
796 |
/** |
|
797 |
* Returns whether all elements of this stream match the provided predicate. |
|
798 |
* May not evaluate the predicate on all elements if not necessary for |
|
21339 | 799 |
* determining the result. If the stream is empty then {@code true} is |
800 |
* returned and the predicate is not evaluated. |
|
17167 | 801 |
* |
802 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
803 |
* terminal operation</a>. |
|
804 |
* |
|
21339 | 805 |
* @apiNote |
806 |
* This method evaluates the <em>universal quantification</em> of the |
|
807 |
* predicate over the elements of the stream (for all x P(x)). If the |
|
808 |
* stream is empty, the quantification is said to be <em>vacuously |
|
809 |
* satisfied</em> and is always {@code true} (regardless of P(x)). |
|
810 |
* |
|
811 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
812 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
813 |
* predicate to apply to elements of this stream |
|
814 |
* @return {@code true} if either all elements of the stream match the |
|
815 |
* provided predicate or the stream is empty, otherwise {@code false} |
|
17167 | 816 |
*/ |
817 |
boolean allMatch(DoublePredicate predicate); |
|
818 |
||
819 |
/** |
|
820 |
* Returns whether no elements of this stream match the provided predicate. |
|
821 |
* May not evaluate the predicate on all elements if not necessary for |
|
21339 | 822 |
* determining the result. If the stream is empty then {@code true} is |
823 |
* returned and the predicate is not evaluated. |
|
17167 | 824 |
* |
825 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
826 |
* terminal operation</a>. |
|
827 |
* |
|
21339 | 828 |
* @apiNote |
829 |
* This method evaluates the <em>universal quantification</em> of the |
|
830 |
* negated predicate over the elements of the stream (for all x ~P(x)). If |
|
831 |
* the stream is empty, the quantification is said to be vacuously satisfied |
|
832 |
* and is always {@code true}, regardless of P(x). |
|
833 |
* |
|
834 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
835 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
836 |
* predicate to apply to elements of this stream |
|
837 |
* @return {@code true} if either no elements of the stream match the |
|
838 |
* provided predicate or the stream is empty, otherwise {@code false} |
|
17167 | 839 |
*/ |
840 |
boolean noneMatch(DoublePredicate predicate); |
|
841 |
||
842 |
/** |
|
843 |
* Returns an {@link OptionalDouble} describing the first element of this |
|
19850 | 844 |
* stream, or an empty {@code OptionalDouble} if the stream is empty. If |
845 |
* the stream has no encounter order, then any element may be returned. |
|
17167 | 846 |
* |
847 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
848 |
* terminal operation</a>. |
|
849 |
* |
|
850 |
* @return an {@code OptionalDouble} describing the first element of this |
|
851 |
* stream, or an empty {@code OptionalDouble} if the stream is empty |
|
852 |
*/ |
|
853 |
OptionalDouble findFirst(); |
|
854 |
||
855 |
/** |
|
856 |
* Returns an {@link OptionalDouble} describing some element of the stream, |
|
857 |
* or an empty {@code OptionalDouble} if the stream is empty. |
|
858 |
* |
|
859 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
860 |
* terminal operation</a>. |
|
861 |
* |
|
862 |
* <p>The behavior of this operation is explicitly nondeterministic; it is |
|
863 |
* free to select any element in the stream. This is to allow for maximal |
|
864 |
* performance in parallel operations; the cost is that multiple invocations |
|
19850 | 865 |
* on the same source may not return the same result. (If a stable result |
866 |
* is desired, use {@link #findFirst()} instead.) |
|
17167 | 867 |
* |
868 |
* @return an {@code OptionalDouble} describing some element of this stream, |
|
869 |
* or an empty {@code OptionalDouble} if the stream is empty |
|
870 |
* @see #findFirst() |
|
871 |
*/ |
|
872 |
OptionalDouble findAny(); |
|
873 |
||
874 |
/** |
|
875 |
* Returns a {@code Stream} consisting of the elements of this stream, |
|
876 |
* boxed to {@code Double}. |
|
877 |
* |
|
19850 | 878 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
879 |
* operation</a>. |
|
880 |
* |
|
17167 | 881 |
* @return a {@code Stream} consistent of the elements of this stream, |
882 |
* each boxed to a {@code Double} |
|
883 |
*/ |
|
884 |
Stream<Double> boxed(); |
|
885 |
||
886 |
@Override |
|
887 |
DoubleStream sequential(); |
|
888 |
||
889 |
@Override |
|
890 |
DoubleStream parallel(); |
|
891 |
||
892 |
@Override |
|
893 |
PrimitiveIterator.OfDouble iterator(); |
|
894 |
||
895 |
@Override |
|
896 |
Spliterator.OfDouble spliterator(); |
|
17195 | 897 |
|
898 |
||
899 |
// Static factories |
|
900 |
||
901 |
/** |
|
902 |
* Returns a builder for a {@code DoubleStream}. |
|
903 |
* |
|
904 |
* @return a stream builder |
|
905 |
*/ |
|
18825
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
906 |
public static Builder builder() { |
17195 | 907 |
return new Streams.DoubleStreamBuilderImpl(); |
908 |
} |
|
909 |
||
910 |
/** |
|
911 |
* Returns an empty sequential {@code DoubleStream}. |
|
912 |
* |
|
913 |
* @return an empty sequential stream |
|
914 |
*/ |
|
915 |
public static DoubleStream empty() { |
|
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
916 |
return StreamSupport.doubleStream(Spliterators.emptyDoubleSpliterator(), false); |
17195 | 917 |
} |
918 |
||
919 |
/** |
|
920 |
* Returns a sequential {@code DoubleStream} containing a single element. |
|
921 |
* |
|
922 |
* @param t the single element |
|
923 |
* @return a singleton sequential stream |
|
924 |
*/ |
|
925 |
public static DoubleStream of(double t) { |
|
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
926 |
return StreamSupport.doubleStream(new Streams.DoubleStreamBuilderImpl(t), false); |
17195 | 927 |
} |
928 |
||
929 |
/** |
|
19850 | 930 |
* Returns a sequential ordered stream whose elements are the specified values. |
17195 | 931 |
* |
932 |
* @param values the elements of the new stream |
|
933 |
* @return the new stream |
|
934 |
*/ |
|
935 |
public static DoubleStream of(double... values) { |
|
936 |
return Arrays.stream(values); |
|
937 |
} |
|
938 |
||
939 |
/** |
|
19850 | 940 |
* Returns an infinite sequential ordered {@code DoubleStream} produced by iterative |
17195 | 941 |
* application of a function {@code f} to an initial element {@code seed}, |
942 |
* producing a {@code Stream} consisting of {@code seed}, {@code f(seed)}, |
|
943 |
* {@code f(f(seed))}, etc. |
|
944 |
* |
|
945 |
* <p>The first element (position {@code 0}) in the {@code DoubleStream} |
|
946 |
* will be the provided {@code seed}. For {@code n > 0}, the element at |
|
947 |
* position {@code n}, will be the result of applying the function {@code f} |
|
948 |
* to the element at position {@code n - 1}. |
|
949 |
* |
|
950 |
* @param seed the initial element |
|
25526 | 951 |
* @param f a function to be applied to the previous element to produce |
17195 | 952 |
* a new element |
953 |
* @return a new sequential {@code DoubleStream} |
|
954 |
*/ |
|
955 |
public static DoubleStream iterate(final double seed, final DoubleUnaryOperator f) { |
|
956 |
Objects.requireNonNull(f); |
|
36223
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
957 |
Spliterator.OfDouble spliterator = new Spliterators.AbstractDoubleSpliterator(Long.MAX_VALUE, |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
958 |
Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) { |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
959 |
double prev; |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
960 |
boolean started; |
17195 | 961 |
|
962 |
@Override |
|
36223
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
963 |
public boolean tryAdvance(DoubleConsumer action) { |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
964 |
Objects.requireNonNull(action); |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
965 |
double t; |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
966 |
if (started) |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
967 |
t = f.applyAsDouble(prev); |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
968 |
else { |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
969 |
t = seed; |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
970 |
started = true; |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
971 |
} |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
972 |
action.accept(prev = t); |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
973 |
return true; |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
974 |
} |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
975 |
}; |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
976 |
return StreamSupport.doubleStream(spliterator, false); |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
977 |
} |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
978 |
|
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
979 |
/** |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
980 |
* Returns a sequential ordered {@code DoubleStream} produced by iterative |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
981 |
* application of a function to an initial element, conditioned on |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
982 |
* satisfying the supplied predicate. The stream terminates as soon as |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
983 |
* the predicate returns false. |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
984 |
* |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
985 |
* <p> |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
986 |
* {@code DoubleStream.iterate} should produce the same sequence of |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
987 |
* elements as produced by the corresponding for-loop: |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
988 |
* <pre>{@code |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
989 |
* for (double index=seed; predicate.test(index); index = f.apply(index)) { |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
990 |
* ... |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
991 |
* } |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
992 |
* }</pre> |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
993 |
* |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
994 |
* <p> |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
995 |
* The resulting sequence may be empty if the predicate does not hold on |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
996 |
* the seed value. Otherwise the first element will be the supplied seed |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
997 |
* value, the next element (if present) will be the result of applying the |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
998 |
* function f to the seed value, and so on iteratively until the predicate |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
999 |
* indicates that the stream should terminate. |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1000 |
* |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1001 |
* @param seed the initial element |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1002 |
* @param predicate a predicate to apply to elements to determine when the |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1003 |
* stream must terminate. |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1004 |
* @param f a function to be applied to the previous element to produce |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1005 |
* a new element |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1006 |
* @return a new sequential {@code DoubleStream} |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1007 |
* @since 9 |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1008 |
*/ |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1009 |
public static DoubleStream iterate(double seed, DoublePredicate predicate, DoubleUnaryOperator f) { |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1010 |
Objects.requireNonNull(f); |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1011 |
Objects.requireNonNull(predicate); |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1012 |
Spliterator.OfDouble spliterator = new Spliterators.AbstractDoubleSpliterator(Long.MAX_VALUE, |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1013 |
Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) { |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1014 |
double prev; |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1015 |
boolean started, finished; |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1016 |
|
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1017 |
@Override |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1018 |
public boolean tryAdvance(DoubleConsumer action) { |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1019 |
Objects.requireNonNull(action); |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1020 |
if (finished) |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1021 |
return false; |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1022 |
double t; |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1023 |
if (started) |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1024 |
t = f.applyAsDouble(prev); |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1025 |
else { |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1026 |
t = seed; |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1027 |
started = true; |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1028 |
} |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1029 |
if (!predicate.test(t)) { |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1030 |
finished = true; |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1031 |
return false; |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1032 |
} |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1033 |
action.accept(prev = t); |
17195 | 1034 |
return true; |
1035 |
} |
|
1036 |
||
1037 |
@Override |
|
36223
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1038 |
public void forEachRemaining(DoubleConsumer action) { |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1039 |
Objects.requireNonNull(action); |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1040 |
if (finished) |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1041 |
return; |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1042 |
finished = true; |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1043 |
double t = started ? f.applyAsDouble(prev) : seed; |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1044 |
while (predicate.test(t)) { |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1045 |
action.accept(t); |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1046 |
t = f.applyAsDouble(t); |
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1047 |
} |
17195 | 1048 |
} |
1049 |
}; |
|
36223
de2976b3614c
8072727: add variation of Stream.iterate() that's finite
tvaleev
parents:
35302
diff
changeset
|
1050 |
return StreamSupport.doubleStream(spliterator, false); |
17195 | 1051 |
} |
1052 |
||
1053 |
/** |
|
21339 | 1054 |
* Returns an infinite sequential unordered stream where each element is |
1055 |
* generated by the provided {@code DoubleSupplier}. This is suitable for |
|
1056 |
* generating constant streams, streams of random elements, etc. |
|
17195 | 1057 |
* |
1058 |
* @param s the {@code DoubleSupplier} for generated elements |
|
21339 | 1059 |
* @return a new infinite sequential unordered {@code DoubleStream} |
17195 | 1060 |
*/ |
1061 |
public static DoubleStream generate(DoubleSupplier s) { |
|
1062 |
Objects.requireNonNull(s); |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
18153
diff
changeset
|
1063 |
return StreamSupport.doubleStream( |
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
1064 |
new StreamSpliterators.InfiniteSupplyingSpliterator.OfDouble(Long.MAX_VALUE, s), false); |
17195 | 1065 |
} |
18820 | 1066 |
|
1067 |
/** |
|
19850 | 1068 |
* Creates a lazily concatenated stream whose elements are all the |
1069 |
* elements of the first stream followed by all the elements of the |
|
21339 | 1070 |
* second stream. The resulting stream is ordered if both |
18820 | 1071 |
* of the input streams are ordered, and parallel if either of the input |
19800 | 1072 |
* streams is parallel. When the resulting stream is closed, the close |
1073 |
* handlers for both input streams are invoked. |
|
18820 | 1074 |
* |
21339 | 1075 |
* @implNote |
1076 |
* Use caution when constructing streams from repeated concatenation. |
|
1077 |
* Accessing an element of a deeply concatenated stream can result in deep |
|
29103
1dbde34ad64c
8073779: StackOverflowError called StackOverflowException in javadoc
igerasim
parents:
25859
diff
changeset
|
1078 |
* call chains, or even {@code StackOverflowError}. |
21339 | 1079 |
* |
18820 | 1080 |
* @param a the first stream |
19850 | 1081 |
* @param b the second stream |
1082 |
* @return the concatenation of the two input streams |
|
18820 | 1083 |
*/ |
1084 |
public static DoubleStream concat(DoubleStream a, DoubleStream b) { |
|
1085 |
Objects.requireNonNull(a); |
|
1086 |
Objects.requireNonNull(b); |
|
1087 |
||
1088 |
Spliterator.OfDouble split = new Streams.ConcatSpliterator.OfDouble( |
|
1089 |
a.spliterator(), b.spliterator()); |
|
19800 | 1090 |
DoubleStream stream = StreamSupport.doubleStream(split, a.isParallel() || b.isParallel()); |
1091 |
return stream.onClose(Streams.composedClose(a, b)); |
|
18820 | 1092 |
} |
18825
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1093 |
|
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1094 |
/** |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1095 |
* A mutable builder for a {@code DoubleStream}. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1096 |
* |
19850 | 1097 |
* <p>A stream builder has a lifecycle, which starts in a building |
1098 |
* phase, during which elements can be added, and then transitions to a built |
|
1099 |
* phase, after which elements may not be added. The built phase |
|
18825
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1100 |
* 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
|
1101 |
* 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
|
1102 |
* stream builder, in the order they were added. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1103 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1104 |
* @see DoubleStream#builder() |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1105 |
* @since 1.8 |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1106 |
*/ |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1107 |
public interface Builder extends DoubleConsumer { |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1108 |
|
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1109 |
/** |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1110 |
* Adds an element to the stream being built. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1111 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1112 |
* @throws IllegalStateException if the builder has already transitioned |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1113 |
* to the built state |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1114 |
*/ |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1115 |
@Override |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1116 |
void accept(double t); |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1117 |
|
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1118 |
/** |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1119 |
* Adds an element to the stream being built. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1120 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1121 |
* @implSpec |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1122 |
* The default implementation behaves as if: |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1123 |
* <pre>{@code |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1124 |
* accept(t) |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1125 |
* return this; |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1126 |
* }</pre> |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1127 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1128 |
* @param t the element to add |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1129 |
* @return {@code this} builder |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1130 |
* @throws IllegalStateException if the builder has already transitioned |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1131 |
* to the built state |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1132 |
*/ |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1133 |
default Builder add(double t) { |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1134 |
accept(t); |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1135 |
return this; |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1136 |
} |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1137 |
|
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1138 |
/** |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1139 |
* Builds the stream, transitioning this builder to the built state. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1140 |
* An {@code IllegalStateException} is thrown if there are further |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1141 |
* 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
|
1142 |
* state. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1143 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1144 |
* @return the built stream |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1145 |
* @throws IllegalStateException if the builder has already transitioned |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1146 |
* to the built state |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1147 |
*/ |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1148 |
DoubleStream build(); |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1149 |
} |
17167 | 1150 |
} |