Reputation: 3380
I'm just learning the java.util.stream
API and I'm looking for a way to quickly fill up a Collection with some data.
I've come up with this code to add 5 random numbers:
List<Integer> lottery = Stream.of(random.nextInt(90), random.nextInt(90), random.nextInt(90),random.nextInt(90),
random.nextInt(90)).collect(Collectors.toList());
That would be however a problem in case I have to add hundreds of items. Is there a more concise way to do it using the java.util.stream
API?
(I could obviously loop in the ordinary way...)
Thanks
Upvotes: 3
Views: 1562
Reputation: 71
This should do:
List<Integer> randomList = new ArrayList<>();
SecureRandom random = new SecureRandom();
IntStream.rangeClosed(1,100).forEach(value->randomList.add(random.nextInt(90)));
Upvotes: 0
Reputation: 35427
Random.ints(long, int, int)
Everyone else on this page is generating the stream because they can, but Java already has such an out of the box method, Random.ints(long, int, int)
, available since Java 8 (and therefore the beginning of streams).
Just use it like this:
List<Integer> lottery = rnd.ints(5, 0, 90)
.boxed()
.collect(Collectors.toList());
The instruction rnd.ints(5L, 0, 90)
means: "create a stream of 5 integers between 0 (included) and 90 (excluded)". So if you want 100 numbers instead of 5, just change 5
to 100
.
Upvotes: 4
Reputation: 73548
In this case the stream API only makes things more complicated vs. a regular loop, but you could create an IntStream
(or Stream<Integer>
depending on where you convert the int
to Integer
) that generates infinite random numbers with
IntStream rndStream = IntStream.generate(() -> rnd.nextInt(90));
Now you can use limit()
to create non-infinite substreams that can be collected, such as
rndStream.limit(5).boxed().collect(Collectors.toList());
However this is just stream trickery and has no advantages over say having a method called List<Integer> getRandoms(int number)
with a regular loop inside.
There's no reason to use the code displayed in this answer as it's less readable and more complex than alternatives. This just demonstrates how to get finite amounts of elements from an infinitely generated stream.
Upvotes: 4