Convert to an Array
Object[] toArray()
Returns an array containing the elements of this stream.
This is a terminal operation.
Returns:
an array containing the elements of this stream
<A> A[] toArray(IntFunction<A[]> generator)
Returns an array containing the elements of this stream, using the provided generator
function to allocate the returned array, as well as any additional arrays that might be required for a partitioned execution or for resizing.
This is a terminal operation.
API Note:
The generator function takes an integer, which is the size of the desired array, and produces an array of the desired size. This can be concisely expressed with an array constructor reference:
Person[] men = people.stream()
.filter(p -> p.getGender() == MALE)
.toArray(Person[]::new);
Type Parameters:
A
- the element type of the resulting array
Parameters:
generator
- a function which produces a new array of the desired type and the provided length
Returns:
an array containing the elements in this stream
Throws:
ArrayStoreException
- if the runtime type of the array returned from the array generator is not a supertype of the runtime type of every element in this stream
case 1:
// streams/RandInts.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package streams;
import java.util.*;
import java.util.stream.*;
public class RandInts {
private static int[] rints = new Random(47).ints(0, 1000).limit(100).toArray();
public static IntStream rands() {
return Arrays.stream(rints);
}
}
Apply a Final Operation to Every Element
S parallel()
Returns an equivalent stream that is parallel. May return itself, either because the stream was already parallel, or because the underlying stream state was modified to be parallel.
This is an intermediate operation.
Returns:
a parallel stream
IntStream parallel()
Description copied from interface: BaseStream
Returns an equivalent stream that is parallel. May return itself, either because the stream was already parallel, or because the underlying stream state was modified to be parallel.
This is an intermediate operation.
Specified by:
parallel
in interface BaseStream<Integer,IntStream>
Returns:
a parallel stream
void forEach(Consumer<? super T> action)
Performs an action for each element of this stream.
This is a terminal operation.
The behavior of this operation is explicitly nondeterministic. For parallel stream pipelines, this operation does not guarantee to respect the encounter order of the stream, as doing so would sacrifice the benefit of parallelism. For any given element, the action may be performed at whatever time and in whatever thread the library chooses. If the action accesses shared state, it is responsible for providing the required synchronization.
Parameters:
action
- a non-interfering action to perform on the elements
void forEachOrdered(Consumer<? super T> action)
Performs an action for each element of this stream, in the encounter order of the stream if the stream has a defined encounter order.
This is a terminal operation.
This operation processes the elements one at a time, in encounter order if one exists. Performing the action for one element happens-before performing the action for subsequent elements, but for any given element, the action may be performed in whatever thread the library chooses.
Parameters:
action
- a non-interfering action to perform on the elements
See Also:
// streams/ForEach.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import static streams.RandInts.*; // note this use
import java.util.*;
import java.util.stream.*;
public class ForEach {
static final int SZ = 14;
public static void main(String[] args) {
rands().limit(SZ).forEach(n -> System.out.format("%d ", n));
System.out.println();
rands().limit(SZ).parallel().forEach(n -> System.out.format("%d ", n));
System.out.println();
rands().limit(SZ).parallel().forEachOrdered(n -> System.out.format("%d ", n));
}
}
/* My Output:
258 555 693 861 961 429 868 200 522 207 288 128 551 589
551 589 258 207 555 693 128 861 288 200 961 429 522 868
258 555 693 861 961 429 868 200 522 207 288 128 551 589
*/
references:
1. On Java 8 - Bruce Eckel
2. https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#toArray--
4. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/streams/RandInts.java
5. https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#ints-int-int-
8. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/streams/ForEach.java
9. https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html#parallel--
10. https://docs.oracle.com/javase/8/docs/api/java/util/stream/BaseStream.html#parallel--