Java.util.Arrays.parallelSetAll(), Arrays.setAll() in Java
Last Updated :
28 Apr, 2022
Prerequisites :
parallelSetAll and setAll are introduced in Arrays class in java 8.
- parallelSetAll(): It set all the element in the specified array in parallel by the function which compute each element.
Syntax:
public static void parallelSetAll(double[] arr, IntToDoubleFunction g)
Parameters :
arr : Array to which the elements to be set
g : It is a function that accepts index of an array
and returns the computed value to that index
parallelSetAll(double[] arr, IntToDoubleFunction g)
parallelSetAll(int[] arr, IntUnaryOperator g)
parallelSetAll(long[] arr, IntToLongFunction g)
parallelSetAll(T[] arr, IntFunction g)
- setAll() : It set all the element in the specified array in by the function which compute each element.
Syntax:
public static void setAll(int[] arr, IntUnaryOperator g)
Parameters :
arr : Array to which the elements to be set
g : It is a function that accepts index of an array
and returns the computed value to that index
setAll(double[] array, IntToDoubleFunction generator)
setAll(int[] array, IntUnaryOperator generator)
setAll(long[] array, IntToLongFunction generator)
setAll(T[] array, IntFunction generator)
parallelSetAll() vs setAll()
Both functions produces same output as can be seen, but parallelSetAll() is consider faster as it performs the changes on the array parallel(i.e. at once) while setAll() updates each indices of the array(i.e. one after another). Though setAll() runs faster on smaller sized array but parallelSetAll() takes over setAll() when the size of array is larger.
Examples
Lets see an example of parallelSetAll(int[] arr, IntUnaryOperator g) and setAll(int[] array, IntUnaryOperator generator)
Java
// Java program to demonstrate setAll()
// and ParallelSetAll()
import java.util.Arrays;
import java.util.function.IntUnaryOperator;
class GFG
{
public static void main(String[] args)
{
// Declaring arrays of integers
int[] arr_parallel1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20 };
int[] arr_parallel2 = Arrays.copyOf(arr_parallel1, arr_parallel1.length);
int[] arr = Arrays.copyOf(arr_parallel1, arr_parallel1.length);
// Applying parallelSetAll on Array arr_parallel1
IntUnaryOperator g = e->
{
if (e % 2 == 0)
return e * e;
else
return e;
};
Arrays.parallelSetAll(arr_parallel1, g);
/* Another way of passing the second argument.
Uncomment to try .
Arrays.parallelSetAll(arr_parallel1, e -> {
if (e % 2 == 0)
return e * e;
else
return e;
}); */
System.out.println("Example 1: Modifying the values at even"
+ " index and storing the square of index");
// Printing the modified array
Arrays.stream(arr_parallel1).forEach(e->System.out.print(e + " "));
// Applying parallelSetAll on Array arr_parallel2
Arrays.parallelSetAll(arr_parallel2, e->
{
if (arr_parallel2[e] % 2 == 0)
return arr_parallel2[e] * arr_parallel2[e];
else
return arr_parallel2[e];
});
System.out.println("\n\nExample 2: Modifying the values when"
+ "even value is encountered");
// Printing the modified array
Arrays.stream(arr_parallel2).forEach(e->System.out.print(e + " "));
// Applying setAll on Array arr
Arrays.setAll(arr, e->
{
if (e % 2 == 0)
return e * e;
else
return e;
});
System.out.println("\n\nExample 3:setAll gives exactly "
+ "same output as parallelSetAll");
// Printing the modified array
Arrays.stream(arr).forEach(e->System.out.print(e + " "));
}
}
Output:
Example 1: Modifying the values at even index and storing the square of index
0 1 4 3 16 5 36 7 64 9 100 11 144 13 196 15 256 17 324 19
Example 2: Modifying the values when even value is encountered
1 4 3 16 5 36 7 64 9 100 11 144 13 196 15 256 17 324 19 400
Example 3:setAll gives exactly same output as parallelSetAll
0 1 4 3 16 5 36 7 64 9 100 11 144 13 196 15 256 17 324 19
Example 2 : We can even even pass arrays of user defined data type. Lets see an example of setAll(T[] array, IntFunction generator) and parallelSetAll(T[] arr, IntFunction g)
Java
// Java program to demonstrate setAll()
// and ParallelSetAll
import java.util.Arrays;
class GFG {
// User Defined class Person
static class Person {
String name;
int age;
// constructor
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
}
public static void main(String[] args)
{
// Declaring Arrays of person
Person p[] = { new Person("samir", 20),
new Person("anil", 25), new Person("amit", 10),
new Person("rohit", 17), new Person("Geek5", 19),
new Person("sumit", 22), new Person("gourav", 24),
new Person("sunny", 27), new Person("ritu", 28) };
// Applying parallelSetAll on p array
Arrays.parallelSetAll(p, e->{
if (p[e].name.startsWith("s"))
return new Person("You are a geek", 100);
else
return new Person(p[e].name, p[e].age);
});
System.out.println("Example 1; Modifying the name that starts with s");
// Printing array elements
Arrays.stream(p).forEach(e->System.out.println(e.name + " " + e.age));
// Declaring another array of person
Person p1[] = { new Person("samir", 16),
new Person("anil", 25), new Person("amit", 10),
new Person("rohit", 17), new Person("Geek5", 19),
new Person("sumit", 16), new Person("gourav", 24),
new Person("sunny", 11), new Person("ritu", 28) };
// Applying setAll on p1
Arrays.setAll(p1, e->{
if (p1[e].age < 18)
return new Person("Teenager", p1[e].age);
else
return new Person(p1[e].name, p1[e].age);
});
System.out.println("\n\nExample 2: Modifying name whose"
+ "age is less than 18");
// Printing array elements
Arrays.stream(p1).forEach(e->System.out.println(e.name + " " + e.age));
}
}
Output:
Example 1; Modifying the name that starts with s
You are a geek 100
anil 25
amit 10
rohit 17
Geek5 19
You are a geek 100
gourav 24
You are a geek 100
ritu 28
Example 2: Modifying name whose age is less than 18
Teenager 16
anil 25
Teenager 10
Teenager 17
Geek5 19
Teenager 16
gourav 24
Teenager 11
ritu 28
Reference :
https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html
Similar Reads
Java.util.ArrayDeque Class in Java | Set 1
java.util.ArrayDeque class describes an implementation of a resizable array structure which implements the Deque interface. Array deques has not immutable and can grow as necessary. They are not thread-safe and hence concurrent access by multiple threads is not supported unless explicitly synchroniz
5 min read
Arrays.parallelPrefix() in Java with Examples
The Arrays.parallelPrefix() method of the Arrays class in Java 8 is used to apply an inclusive prefix operation on an array in parallel. It performs operations like addition, multiplication, or other binary operations in parallel to speed up the processing of large arrays.Example:Below is a simple e
6 min read
util.Arrays vs reflect.Array in Java with Examples
Array class in java.lang.reflect package is a part of the Java Reflection. This class provides static methods to create and access Java arrays dynamically. It is a final class, which means it canât be instantiated or changed. Only the methods of this class can be used by the class name itself. On th
2 min read
Serial Sort v/s Parallel Sort in Java
We often need to sort array while programming. For this, we use inbuilt method provided by Java in Arrays class i.e sort(). sort() method uses merge sort or Time Sort to sort the array elements. In both the cases sort() method sequentially sort the elements of an array. In Java 8, there is a new API
4 min read
Parallel Data Processing in Java | Set 1
We know that new Stream in Java (introduced in Java 8) interface let us manipulate collections of data in a declarative way. In this topic, we will discover how the Stream interface gives us the opportunity to execute operations in parallel on a collection of data without much effort. It lets us dec
2 min read
CopyOnArrayList replaceAll() method in Java with Examples
The java.util.concurrent.CopyOnArrayList.replaceAll() method in Java replaces each element of this list with the result of applying the operator to the element. Syntax: public void replaceAll(UnaryOperator operator) Parameters: This method accepts a mandatory parameter operator which is to be applie
1 min read
Array setFloat() method in Java
The java.lang.reflect.Array.setFloat() is an inbuilt method in Java and is used to change a specified float value to a specified index of a given object array. Syntax: Array.setFloat(Object []array, int index, float value) Parameter: This method takes three parameters: array: This is an array of typ
3 min read
Array setDouble() method in Java
The java.lang.reflect.Array.setDouble() is an inbuilt method in Java and is used to set a specified double value to a specified index of a given object array. Syntax: Array.setDouble(Object []array, int index, double value) Parameter: This method takes three parameters: array: This is an array of ty
3 min read
ArrayList removeAll() Method in Java with Examples
The removeAll() method of the ArrayList class in Java is used to remove all elements of an ArrayList that are specified in another collection or within the same list itself.Example 1: Here, we use the removeAll() method to remove all elements from an ArrayList by passing the same list as an argument
3 min read
Array setBoolean() Method in Java with Examples
The java.lang.reflect.Array.setBoolean() method is an inbuilt method used to set a specified Boolean value to a specified index of a given object array. Syntax: Array.setBoolean(Object []array, int index, boolean value) Parameter: This method takes three parameters: array: array of type Object which
3 min read