Java Arrays parallelSetAll​(long[] array, IntToLongFunction generator) Method



Description

The Java Arrays parallelSetAll​(long[] array, IntToLongFunction 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​(long[] array, IntToLongFunction generator) method

public static void parallelSetAll​(long[] array, IntToLongFunction 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 Longs Example

The following example shows the usage of Java Arrays parallelSetAll(long[] array, IntToLongFunction generator) method. First, we've created an array of longs, 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
      long arr[] = { 1L, 2L, 3L, 5L, 8L };

      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 Longs Using Generator Function Example

The following example shows the usage of Java Arrays parallelSetAll(long[] array, IntToLongFunction generator) method. First, we've created an array of longs, printed the original array. A IntToLongFunction 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.IntToLongFunction;

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

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

      IntToLongFunction 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 Longs Using Generator Function Example

The following example shows the usage of Java Arrays parallelSetAll(long[] array, IntToLongFunction generator) method. First, we've created an array of longs, 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
      long arr[] = { 1L, 2L, 3L, 5L, 8L };

      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 long generator(long a) {
      return a + 10L;
   }
}

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