How to populate Array with Streams in Java?

Introduction

In this blog we will learn how to populate an array using java streams.

For populating array we can use Supplier functional interface.

Supplier<T> only returns value.

For generating value we can Random class in Java.

Let us take a look with an example.

Populate an int array using java streams

In the below example we generate random number using Random class in the IntSupplier.

1
IntSupplier data = () -> new Random().nextInt(1,1000);

IntStream.generate(Supplier) takes in the supplier to generate the data.

We should limit the number of records we want to generate for our array using limit method of stream.

If we want distinct values we can use distinct() method or else we can skip this step if we are comfortable with duplicate values.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
package com.examples;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.function.IntSupplier;
import java.util.stream.IntStream;

public class App {

    public static void main(String[] args) {

        IntSupplier data = () -> new Random().nextInt(1,1000);
        int[] result = IntStream.generate(data)
                .limit(7)
                .distinct()
                .toArray();
        System.out.println(Arrays.toString(result)); //[64, 923, 558, 922, 310, 112, 81]
    }
}

Similarly we can populate different types of array using Java Streams for double and long

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.examples;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.function.DoubleSupplier;
import java.util.function.IntSupplier;
import java.util.function.LongSupplier;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;

public class App {

    public static void main(String[] args) {

        IntSupplier data = () -> new Random().nextInt(1,1000);
        int[] result = IntStream.generate(data)
                .limit(7)
                .distinct()
                .toArray();
        System.out.println(Arrays.toString(result));  // [64, 923, 558, 922, 310, 112, 81]

        DoubleSupplier doubleSupplier = () -> new Random().nextDouble(1,1000);
        double[] doubleResult = DoubleStream.generate(doubleSupplier)
                .limit(7)
                .distinct()
                .toArray();
        System.out.println(Arrays.toString(doubleResult)); // [844.9316629814911, 431.78154443738237, 637.2903893743176, 348.72101068392203, 473.8227507097683, 466.4165080521481, 503.2107783929512]

        LongSupplier longSupplier = () -> new Random().nextLong(1,1000);
        long[] longResult = LongStream.generate(longSupplier)
                .limit(7)
                .distinct()
                .toArray();
        System.out.println(Arrays.toString(longResult)); //[833, 219, 169, 449, 279, 717, 533]
    }
}

Conclusion

So we have seen how to populate an array using java streams with various examples.

I hope this was useful!