Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
array
parallel
communitycreator

What is Arrays.parallelSetAll in Java?

abhilash

What is 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;

Syntax


public static <T> void parallelSetAll(T[] array, IntFunction<? extends T> generator)

Parameters

  • <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.

Overloaded methods

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.

  1. parallelSetAll(T[] array, IntFunction<? extends T> generator)

  2. parallelSetAll(int[] array, IntUnaryOperator generator)

  3. parallelSetAll(long[] array, IntToLongFunction generator)

  4. parallelSetAll(double[] array, IntToDoubleFunction generator)

Code

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+" ");
    }
}

RELATED TAGS

java
array
parallel
communitycreator
RELATED COURSES

View all Courses

Keep Exploring