📜  Java.util.Arrays.parallelSetAll(), Java中的 Arrays.setAll()

📅  最后修改于: 2022-05-13 01:54:36.946000             🧑  作者: Mango

Java.util.Arrays.parallelSetAll(), Java中的 Arrays.setAll()

先决条件:

  • Java 8 中的 Lambda 表达式
  • IntUnaryOperator 接口

Java 8 的 Arrays 类中引入了 parallelSetAll 和 setAll。

  • parallelSetAll():通过计算每个元素的函数并行设置指定数组中的所有元素。
    句法:
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() :它通过计算每个元素的函数设置指定数组中的所有元素。
    句法:
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() 与 setAll()

可以看出,这两个函数产生相同的输出,但 parallelSetAll() 被认为更快,因为它在数组上并行执行更改(即一次),而 setAll() 更新数组的每个索引(即一个接一个)。虽然 setAll() 在较小的数组上运行得更快,但是当数组的大小较大时,parallelSetAll() 会接管 setAll()。

例子

让我们看一个parallelSetAll(int[] arr, IntUnaryOperator g)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 + " "));
    }
}


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


输出:

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  

示例 2:我们甚至可以传递用户定义数据类型的数组。让我们看一个setAll(T[] array, IntFunction generator)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));
    }
}

输出:

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