java – How to generate random array of ints using Java 8 Stream API?

Generate random numbers in the range from 0 to 350, limiting the result to 10 and collecting as a list. Later it could be typed.

However, there are no guarantees as to the type, mutability, serializability, or thread safety of the returned list.

List<Object> numbers =  new Random().ints(0,350).limit(10).boxed().collect(Collectors.toList());

and to get the int use array

int[] numbers =  new Random().ints(0,350).limit(10).toArray();

Leave a comment