
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Arrays parallelSetAll(T[] array, IntFunction<? extends T> generator) Method
Description
The Java Arrays parallelSetAll(T[] array, IntFunction<? extends T> 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(T[] array, IntFunction<? extends T> generator) method
public static <T> void parallelSetAll(T[] array, IntFunction<? extends T> 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 Integers Example
The following example shows the usage of Java Arrays parallelSetAll(T[] array, IntFunction<? extends T> generator) method. First, we've created an array of Integer objects, 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 Integer arr[] = { 1, 2, 3, 5, 8 }; System.out.print("Original Array: ["); // print the array for (Integer 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 (Integer 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 Integers Using Generator Function Example
The following example shows the usage of Java Arrays parallelSetAll(T[] array, IntFunction<? extends T> generator) method. First, we've created an array of Integer objects, printed the original array. A IntFunction 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.IntFunction; public class ArrayDemo { public static void main(String[] args) { // initialize array Integer arr[] = { 1, 2, 3, 5, 8 }; System.out.print("Original Array: ["); // print the array for (Integer i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println("]"); IntFunction<Integer> generator = i -> arr[i] + 10; Arrays.parallelSetAll(arr, generator); System.out.print("Modified Array: ["); // print the array for (Integer 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 Integers Using Generator Function Example
The following example shows the usage of Java Arrays parallelSetAll(T[] array, IntFunction<? extends T> generator) method. First, we've created an array of Integer objects, 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 Integer arr[] = { 1, 2, 3, 5, 8 }; System.out.print("Original Array: ["); // print the array for (Integer 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 (Integer 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 ]