
- 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(double[] array, IntToDoubleFunction generator) Method
Description
The Java Arrays parallelSetAll(double[] array, IntToDoubleFunction 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(double[] array, IntToDoubleFunction generator) method
public static void parallelSetAll(double[] array, IntToDoubleFunction 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 doubles Example
The following example shows the usage of Java Arrays parallelSetAll(double[] array, IntToDoubleFunction generator) method. First, we've created an array of doubles, 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 double arr[] = { 1.0, 2.0, 3.0, 5.0, 8.0 }; 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.0 2.0 3.0 5.0 8.0 ] Modified Array: [11.0 12.0 13.0 15.0 18.0 ]
Modifying array of doubles Using Generator Function Example
The following example shows the usage of Java Arrays parallelSetAll(double[] array, IntToDoubleFunction generator) method. First, we've created an array of doubles, printed the original array. A IntToDoubleFunction 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.IntToDoubleFunction; public class ArrayDemo { public static void main(String[] args) { // initialize array double arr[] = { 1.0, 2.0, 3.0, 5.0, 8.0 }; System.out.print("Original Array: ["); // print the array for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println("]"); IntToDoubleFunction 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.0 2.0 3.0 5.0 8.0 ] Modified Array: [11.0 12.0 13.0 15.0 18.0 ]
Modifying array of doubles Using Generator Function Example
The following example shows the usage of Java Arrays parallelSetAll(double[] array, IntToDoubleFunction generator) method. First, we've created an array of doubles, 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 double arr[] = { 1.0, 2.0, 3.0, 5.0, 8.0 }; 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 double generator(double a) { return a + 10; } }
Output
Let us compile and run the above program, this will produce the following result −
Original Array: [1.0 2.0 3.0 5.0 8.0 ] Modified Array: [11.0 12.0 13.0 15.0 18.0 ]