Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an int[] with fixed length and specific number with java stream? [duplicate]

I know how to get an int[] with a range of numbers:

int[] array = IntStream.of(0, 3).toArray();

But how can I get it with fixed length and one specific number?

like image 343
xingbin Avatar asked Dec 06 '22 11:12

xingbin


1 Answers

IntStream.generate(() -> x).limit(y)

is what you need. Replace x and y with any number you like and you will produce a stream that has y lots of the number x.

You can obviously then call toArray or do whatever operation you want.

IntStream.generate creates an infinite stream using the supplier.

like image 186
Sweeper Avatar answered Jan 12 '23 15:01

Sweeper