There is a new method
setAll in the
java.util.Arrays class that will perform a bulk assignment of every element of an array. For example, the following code:
String array = ... ;
will set each element of a String array to the string representation of the element’s index.
There are also overloads for setting all elements of
double arrays, as well
parallelSetAll methods that run in parallel.
Unfortunately, there are no similar methods that deal with array subranges, nor are there methods that can deal with arrays whose element type
is something other than a reference type or the three specialized primitives
double. Fortunately, it’s fairly simple to use
IntStream.range to generate a range of array indexes and to use stream operations to perform the array operation that you want.
For example, to increment the value within a subrange of an
int array, do the following:
int array = ... ;
.forEach(i -> array[i]++);
To set the elements of a byte array, do the following:
byte bytes = ... ;
.forEach(i -> bytes[i] = (byte)i);
To create an
IntStream from a
char array, do the following:
char chars = ... ;
.map(i -> chars[i])