How to generate random array of ints using Stream API Java 8?
If you want primitive int
values, do not call IntStream::boxed
as that produces Integer
objects by boxing.
Simply use Random::ints
which returns an IntStream
:
int[] array = new Random().ints(size, lowBound, highBound).toArray();
To generate random numbers from range 0 to 350, limiting the result to 10, and collect as a List. Later it could be typecasted.
However, There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned.
List<Object> numbers = new Random().ints(0,350).limit(10).boxed().collect(Collectors.toList());
and to get thearray of int use
int[] numbers = new Random().ints(0,350).limit(10).toArray();