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
- Variations :
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
- Variations :
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)
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 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