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

  • A+
Category:Languages

This question already has an answer here:

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?

 


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.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: