Java.util.Arrays.parallelSetAll(), Arrays.setAll() in Java

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)

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 + " "));
    }
}

chevron_right


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)

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 paralleSetAll 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));
    }
}

chevron_right


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
This article is contributed by Sumit Ghosh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up