Signup/Sign In

Java Arrays parallelSetAll() Method

In this tutorial, we will learn parallelSetAll() method. Unlike the parallelPrefix() method, parallelSetAll() method deals with the current element only without bothering about other elements. It is the Arrays class method and used to set elements in an array.

Syntax

parallelSetAll(datatype[] array, IntToDoubleFunction generator)

List of Overloading Methods of parallelSetAll() Method

This table contains all the overloaded variants of parallelSetAll() method.

Method Description

static void parallelSetAll(double[] array, IntToDoubleFunction generator)

This method sets all elements of the specified array, in parallel, using the provided generator function to compute each element.

static void parallelSetAll(int[] array, IntUnaryOperator generator)

This method sets all elements of the specified array, in parallel, using the provided generator function to compute each element.

static void parallelSetAll(long[] array, IntToLongFunction generator)

This method sets all elements of the specified array, in parallel, using the provided generator function to compute each element.

static <T> void parallelSetAll(T[] array, IntFunction<? extends T> generator)

This method sets all elements of the specified array, in parallel, using the provided generator function to compute each element.

Example of parallelSetAll()

In this example, we have an array of elements from 0 to 15. We are passing this array to the method parallelSetAll with the IntUnaryOperator which defines what operation is going to perform on array elements. When we want to change the individual elements of an array in a particular pattern then this method is very useful. for e.g. array in which the height of students is stored and now we want to change them in feet. To do so we can build IntUnaryOperator.

import java.util.Arrays;
import java.util.function.IntUnaryOperator;
class StudyTonight { 
	public static void main(String args[]) 
	{ 
        int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};        
        IntUnaryOperator op = n-> 
        { 
            if (n % 2 == 0) 
                return n * n; 
            else
                return n; 
        }; 
        Arrays.parallelSetAll(arr, op); 
        
        for(int num:arr)
        {
        	System.out.print(num+" ");
        }
	} 
}


0 1 4 3 16 5 36 7 64 9 100 11 144 13 196 15

Example of all overloading methods of parallelSetAll()

In this example, we implemented all the overloading() methods of parallelSetAll() method. This method is very much similar to SetAll() method. This example illustrates IntUnaryOperator, IntToLongFunction, IntToDoubleFunction.

import java.util.Arrays;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		//Example of static void parallelSetAll(int[] array, IntUnaryOperator generator)
		int array1[] = new int[10];
		Arrays.parallelSetAll(array1, (index)-> index+index);
		System.out.println(Arrays.toString(array1));
		
		//Example of static void parallelSetAll(long[] array, IntToLongFunction generator)
		long array2[] = new long[10];
		Arrays.parallelSetAll(array2, (index)-> index+index);
		System.out.println(Arrays.toString(array2));
		
		//Example of static void parallelSetAll(double[] array, IntToDoubleFunction generator)
		double array3[] = new double[10];
		Arrays.parallelSetAll(array3, (index)-> index+index);
		System.out.println(Arrays.toString(array3));
	}
}


[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[0.0, 1.0, 4.0, 9.0, 16.0, 25.0, 36.0, 49.0, 64.0, 81.0]

Conclusion:

In this tutorial, we learned how we can use parallelSetAll() method to change the array elements in a specific manner individually. This method also overloads with some variations to provide flexibility to perform manipulation on all types of arrays.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.