Java Arrays.setAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

Definition

In Java, Arrays.setAll(array, generator) replaces every element of array with generator applied to that element's index. With i -> i * 2, index 0 gets 0, index 1 gets 2, index 2 gets 4, and so on.

It is a compact replacement for a for loop that assigns a[i] = something(i). Because the function receives the index, it can also read a[i] or another array at the same index, which makes in-place transformations easy.

A static method of java.util.Arrays (Java 8+): call it on the class, as in Arrays.setAll(...); there is no Arrays object to create. It arrived with lambdas in Java 8.

Syntax

Pass the array and a lambda from index to value.

Java
int[] squares = new int[5];
Arrays.setAll(squares, i -> i * i);        // [0, 1, 4, 9, 16]
String[] ids = new String[3];
Arrays.setAll(ids, i -> "id" + i);         // [id0, id1, id2]
CallWhat it does
Arrays.setAll(int[] a, i -> ...)Generator is an IntUnaryOperator (index to int).
Arrays.setAll(long[] a, i -> ...)Generator is an IntToLongFunction.
Arrays.setAll(double[] a, i -> ...)Generator is an IntToDoubleFunction.
Arrays.setAll(T[] a, i -> ...)Generator is an IntFunction<? extends T>.

There are only four overloads: int[], long[], double[], and object arrays. There is no char[], boolean[], or range version; use a loop for those.

Library declarations
Java
// java.util.Arrays (since Java 8)
public static void setAll(int[] array, IntUnaryOperator generator)
public static void setAll(long[] array, IntToLongFunction generator)
public static void setAll(double[] array, IntToDoubleFunction generator)
public static <T> void setAll(T[] array, IntFunction<? extends T> generator)

Parameters

ParameterDescription
arrayThe array to fill. Must not be null.
generatorA function that receives an index and returns the value for that index. Must not be null.

Return Value

Return ValueDescription
voidNothing. Every element of array is overwritten.

Compare with Similar Methods

Choosing how to fill an array:

MethodHow it differs
Arrays.fill(a, v)The same value everywhere; no function needed.
Arrays.parallelSetAll(a, f)Same idea, computed on several threads for big arrays.
for (int i = 0; ...)Needed when an element depends on earlier elements or for char[] and boolean[].
list.replaceAll(op)The ArrayList counterpart that maps each element to a new value.

Java Examples

Example 1 — A Times Table and an Alternating Pattern

Fill one array with multiples of 7 and another with alternating 1s and 0s.

Java
import java.util.Arrays;

public class ArraysSetAllDemo {
    public static void main(String[] args) {
        int[] table = new int[10];
        Arrays.setAll(table, i -> (i + 1) * 7);
        System.out.println("7 times table: " + Arrays.toString(table));

        int[] pattern = new int[6];
        Arrays.setAll(pattern, i -> i % 2 == 0 ? 1 : 0);
        System.out.println("pattern:       " + Arrays.toString(pattern));
    }
}

How It Works

  1. Index in, value out. The lambda receives 0 to 9; adding 1 before multiplying gives 7 for index 0 and 70 for index 9.
  2. No loop variable to manage. setAll visits every index of table, so there is no i < table.length condition to get wrong.
  3. Conditions work too. i % 2 == 0 ? 1 : 0 gives 1 at even indexes and 0 at odd ones.
  4. Overload by array type. Both arrays are int[], so both lambdas are treated as IntUnaryOperator.

Example 2 — Change Every Element Using Its Current Value

Halve each price and upper-case each name by reading the element at the same index.

Java
import java.util.Arrays;

public class ArraysSetAllTransform {
    public static void main(String[] args) {
        double[] prices = {20.0, 45.0, 8.0};
        Arrays.setAll(prices, i -> prices[i] * 0.5);
        System.out.println("half price: " + Arrays.toString(prices));

        String[] names = {"ada", "alan", "grace"};
        Arrays.setAll(names, i -> names[i].toUpperCase());
        System.out.println("upper case: " + Arrays.toString(names));
    }
}

How It Works

  1. Reading the old value. prices[i] * 0.5 uses the current element at index i to compute its replacement.
  2. Exact halves. 20.0, 45.0, and 8.0 halve to 10.0, 22.5, and 4.0, all exactly representable as double.
  3. Objects need IntFunction. names is a String[], so the lambda is an IntFunction<String> that returns the upper-case version.
  4. Effectively final. The lambdas can use prices and names because those variables are never reassigned.

Example 3 — Build an Identity Matrix and Powers of Ten

Create each row of a 2D array with a block lambda, and fill a long[].

Java
import java.util.Arrays;

public class ArraysSetAllIdentity {
    public static void main(String[] args) {
        int[][] identity = new int[3][];
        Arrays.setAll(identity, i -> {
            int[] row = new int[3];
            row[i] = 1;
            return row;
        });
        System.out.println(Arrays.deepToString(identity));

        long[] big = new long[4];
        Arrays.setAll(big, i -> (long) Math.pow(10, i + 9));
        System.out.println(Arrays.toString(big));
    }
}

How It Works

  1. Rows are objects. identity is an array of int[], so the object overload runs and the lambda must return an int[].
  2. A fresh row per index. The block lambda creates a new row each time and puts a 1 on the diagonal at position i.
  3. Values beyond int. 1010 and larger do not fit in an int, so big is a long[] and the lambda is an IntToLongFunction.
  4. Exact powers. Math.pow(10, n) is exact for these small whole-number powers, so the cast to long loses nothing.

Frequently Asked Questions

It sets every element of an array to the result of a function applied to the element's index, for example Arrays.setAll(a, i -> i * i).
Java 8, together with lambdas and the functional interfaces it uses.
No. It only has overloads for int[], long[], double[], and object arrays. Use a for loop for other types.
Yes. Inside the lambda you can read array[i] for the same index, for example Arrays.setAll(a, i -> a[i] * 2).
fill writes one value into every slot. setAll calls a function for each index, so every slot can get a different value.

Next: Arrays.sort()

Sort an array in place, whole or by range, with an optional Comparator.

sort() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful