Pages

Tuesday, January 26, 2021

Java Arrays.setAll() Examples To Modify Existing Array

1. Overview

In this article, You'll learn how to use JDK 1.8 Arrays.setAll() method to modify the existing values of the array with a unique generator function. Arrays class is part of java.util package.

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

In the previous article, We've seen how to set the whole array with the same value using Arrays.fill() method?

Java Arrays.setAll() Examples


2. Arrays.setAll() Syntax

This is an overloaded method and available in 4 versions as below.

Works for integer, double and long arrays. And also, this method provides supports for Generics.

public static void setAll(double[] array,
                          IntToDoubleFunction generator)


public static void setAll(int[] array,
                          IntUnaryOperator generator)


public static void setAll(long[] array,
                          IntToLongFunction generator)
						  

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

3. Arrays.setAll() Example - int[] Array


Below example, the program generates even numbers from the array.
package com.javaprogramto.arrays.setall;

import java.util.Arrays;

public class IntArraySetAllExample {

    public static void main(String[] args) {

        int[] intArray = new int[7];

        System.out.println("original int array : "+ Arrays.toString(intArray));

        Arrays.setAll(intArray, i -> (i + 1) * 2);

        System.out.println("array.setAll(int) output: "+ Arrays.toString(intArray));
    }
}

Output:
original int array : [0, 0, 0, 0, 0, 0, 0]
array.setAll(int) output: [2, 4, 6, 8, 10, 12, 14]

4. Arrays.setAll() Example - double[] Array


Example program to set the double values using Arrays.setAll(double[]. IntToDoubleFunction) method and passing the IntToDoubleFunction as Lambda Expression.
package com.javaprogramto.arrays.setall;

import java.util.Arrays;

public class DoubleArraySetAllExample {

    public static void main(String[] args) {

        // Creating a double array with default values
        double[] doubleArray = new double[7];

        System.out.println("original double array with default values : "+ Arrays.toString(doubleArray));

        // setting values to doubleArray using setAll() method
        Arrays.setAll(doubleArray, i -> i + 10.5);

        System.out.println("Double array.setAll(double) output: "+ Arrays.toString(doubleArray));
    }
}
Output:
original double array with default values : [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Double array.setAll(double) output: [10.5, 11.5, 12.5, 13.5, 14.5, 15.5, 16.5]

5. Arrays.setAll() Example - long[] Array


setAll() method takes long array with generator IntToLongFunction.
package com.javaprogramto.arrays.setall;

import java.util.Arrays;

public class LongArraySetAllExample {

    public static void main(String[] args) {

        // Creating a long array with default values
        long[] longArray = new long[7];

        System.out.println("original long array with default values : "+ Arrays.toString(longArray));

        // setting values to longArray using setAll() method
        Arrays.setAll(longArray, i -> i + 10 * 20);

        System.out.println("Long array.setAll(long) output: "+ Arrays.toString(longArray));
    }
}

6. Arrays.setAll() Example - String[] Array


Any Array type can be passed to setAll() method because it supports for genetics. Let us try passing the String Array.
public class StringArraySetAllExample {

    public static void main(String[] args) {

        // Creating a String array with default values
        String[] stringArray = new String[7];

        System.out.println("original String array with default values : "+ Arrays.toString(stringArray));

        // setting values to stringArray using setAll() method
        Arrays.setAll(stringArray, i -> i + 10 * 20);

        System.out.println("String array.setAll(String) output: "+ Arrays.toString(stringArray));
    }
}

Output:
original String array with default values : [null, null, null, null, null, null, null]
Exception in thread "main" java.lang.ArrayStoreException: java.lang.Integer
	at java.base/java.util.Arrays.setAll(Arrays.java:5248)
	at com.javaprogramto.arrays.setall.StringArraySetAllExample.main(StringArraySetAllExample.java:15)

Throws the runtime exception saying "ArrayStoreException: java.lang.Integer" that means it can not store the Integer values inside String Array.

So, we should return only String from inside generator logic.

Next, Change the logic to return string as below and execute the program.
Arrays.setAll(stringArray, i -> "Index " + i);
This code returns the Index number of each index and sets it into String Array.

Output:
String array.setAll(String) output: [Index 0, Index 1, Index 2, Index 3, Index 4, Index 5, Index 6]

7. Arrays.setAll() Example with Custom Object Employee[] Array


Last, look at the user-defined array with the Arrays.setAll() method.
import com.javaprogramto.models.Employee;

import java.util.Arrays;

public class EmployeeArraySetAllExample {

    public static void main(String[] args) {

        // Creating a Employee array with default values
        Employee[] employeeArray = new Employee[7];

        System.out.println("original Employee array with default values : "+ Arrays.toString(employeeArray));

        // setting values to employeeArray using setAll() method
        Arrays.setAll(employeeArray, i -> new Employee(i+1, "Index "+i, i+30));

        System.out.println("Employee array.setAll(Employee) output: "+ Arrays.toString(employeeArray));
    }
}

Output:
original Employee array with default values : [null, null, null, null, null, null, null]
Employee array.setAll(Employee) output: [Employee{id=1, fullName='Index 0', age=30}, Employee{id=2, fullName='Index 1', age=31}, Employee{id=3, fullName='Index 2', age=32}, Employee{id=4, fullName='Index 3', age=33}, Employee{id=5, fullName='Index 4', age=34}, Employee{id=6, fullName='Index 5', age=35}, Employee{id=7, fullName='Index 6', age=36}]

Employee custom generator logic creates new Employee objects and sets the data. Original employeeArray is not having any values and after calling setAll() method, employee objects are set to it.

8. Conclusion


In this article, You've seen how to modify the values of the existing array using a unique generator function.

All examples are shown with the Array.setAll() method for int, long, double, String, and Employee arrays.

If the generator function returns a different type of object than the original array type then it will throw ArrayStoreException. 

All Examples shown are over GitHub.


No comments:

Post a Comment

Please do not add any spam links in the comments section.