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