Java Arrays parallelSetAll​(int[] array, IntUnaryOperator generator) Method



Description

The Java Arrays parallelSetAll​(int[] array, IntUnaryOperator generator) method sets all elements of the specified array, in parallel, using the provided generator function to compute each element. Being parallel, set all is generally more efficient than sequential looping set all for large arrays.

Declaration

Following is the declaration for java.util.Arrays.parallelSetAll​(int[] array, IntUnaryOperator generator) method

public static void parallelSetAll​(int[] array, IntUnaryOperator generator)

Parameters

  • array − This is the array to be initialized.

  • generator − This is a function accepting an index and producing the desired value for that position.

Return Value

This method is not returning anything.

Exception

  • NullPointerException − if generator is null.

Modifying array of ints Example

The following example shows the usage of Java Arrays parallelSetAll(int[] array, IntUnaryOperator generator) method. First, we've created an array of ints, printed the original array. Using parallelSetAll() method, array is modified to have updated results. Modified array is printed thereafter.

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
      // initialize array
      int arr[] = { 1, 2, 3, 5, 8 };

      System.out.print("Original Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");

      Arrays.parallelSetAll(arr, i -> arr[i] + 10 );

      System.out.print("Modified Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Original Array: [1 2 3 5 8 ]
Modified Array: [11 12 13 15 18 ]

Modifying array of ints Using Generator Function Example

The following example shows the usage of Java Arrays parallelSetAll(int[] array, IntUnaryOperator generator) method. First, we've created an array of ints, printed the original array. A IntUnaryOperator function is created and passed to parallelSetAll() method to modify the array accordingly. Array is modified to have updated results. Modified array is printed thereafter.

package com.tutorialspoint;

import java.util.Arrays;
import java.util.function.IntUnaryOperator;

public class ArrayDemo {
   public static void main(String[] args) {
      // initialize array
      int arr[] = { 1, 2, 3, 5, 8 };

      System.out.print("Original Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");

      IntUnaryOperator generator = i -> arr[i] + 10;

      Arrays.parallelSetAll(arr, generator);

      System.out.print("Modified Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Original Array: [1 2 3 5 8 ]
Modified Array: [11 12 13 15 18 ]

Modifying array of ints Using Generator Function Example

The following example shows the usage of Java Arrays parallelSetAll(int[] array, IntUnaryOperator generator) method. First, we've created an array of ints, printed the original array. A generator function is created and passed to parallelSetAll() method to modify the array accordingly. Array is modified to have updated results. Modified array is printed thereafter.

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
      // initialize array
      int arr[] = { 1, 2, 3, 5, 8 };

      System.out.print("Original Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");

      Arrays.parallelSetAll(arr, i -> generator(arr[i]));

      System.out.print("Modified Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");
   }
   
   private static int generator(int a) {
      return a + 10;
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Original Array: [1 2 3 5 8 ]
Modified Array: [11 12 13 15 18 ]
java_util_arrays.htm
Advertisements