Java Reference
In-Depth Information
If you can get away with using stateless operations, then you will get better parallel perform-
ance. Examples of stateless operations include
map
,
filter
, and
flatMap
;
sorted
,
dis-
tinct
, and
limit
are stateful.
NOTE
Performance-test your own code. The advice in this section offers rules of thumb about
what performance characteristics should be investigated, but nothing beats measuring and
profiling.
Parallel Array Operations
Java 8 includes a couple of other parallel array operations that utilize lambda expressions
outside of the streams framework. Like the operations on the streams framework, these are
data parallel operations. Let's look at how we can use these operations to solve problems that
are hard to do in the streams framework.
These operations are all located on the utility class
Arrays
, which also contains a bunch of
other useful array-related functionality from previous Java versions. There is a summary in
Table 6-1. Parallel operations on arrays
Name
Operation
parallelPrefix
Calculates running totals of the values of an array given an arbitrary function
parallelSetAll
Updates the values in an array using a lambda expression
Sorts elements in parallel
parallelSort
You may have written code similar to
Example 6-7
before, where you initialize an array us-
ing a
for
loop. In this case, we initialize every element to its index in the array.
Example 6-7. Initializing an array using a for loop
public
public static
static
double
double
[]
imperativeInitilize
(
int
int
size
) {
double
double
[]
values
=
new
new
double
double
[
size
];