parallelSetAll
?The parallelSetAll
method of the Arrays
class is used to set all elements of the specified array, in parallel. It uses the provided generator function to compute each element.
Arrays
is defined in the util
package in Java
, so you must import
the util
package before you can use the parallelSetAll
function, as shown below.
import java.util.Arrays;
public static <T> void parallelSetAll(T[] array, IntFunction<? extends T> generator)
<T>
: type of elements of the array.T[] array
: array to be initialized.IntFunction<? extends T> generator
: a function accepting an index and producing the desired value for that position.The following are some overloaded methods for parallelSetAll
in Java.
These overloaded methods provide the same name of the method, but with different parameters and return types.
parallelSetAll(T[] array, IntFunction<? extends T> generator)
parallelSetAll(int[] array, IntUnaryOperator generator)
parallelSetAll(long[] array, IntToLongFunction generator)
parallelSetAll(double[] array, IntToDoubleFunction generator)
In the below code, we first define an integer
array and generator function that squares the index value of every element in the array.
Next, we pass the array and the generator function to the Arrays.parallelSetAll
method to set the elements of the array.
import java.util.Arrays;import java.util.function.IntUnaryOperator;public class Main {public static void main(String[] args) {int array[] = new int[10];IntUnaryOperator generatorFunction = operand -> operand * operand;Arrays.parallelSetAll(array, generatorFunction);for(int num:array) System.out.print(num+" ");}}