I am trying to get 8 random int using stream but the problem with the code below is that distinct() removes duplicate which will not give me 8 int if there was any duplicate.
Goal:
1.Get 8 random int (no duplicate)
2.Add to List
3.Sort the first 7 int.
I know Collections.sort(winlist.subList(0, 6)); works for sorting but im trying to see if it can be done with just stream.
new Random()
.ints (8, 0, 64)
.distinct()
.sorted()
.forEach (Integer -> System.out.print (Integer + "\n"));
Use an endless stream and limit it after the distinct operation.
new Random().ints(0, 64).distinct().limit(8).sorted().forEach(System.out::println);
This will print 8 random integers from the range [0,64) in sorted order.
To sort only the first 7 numbers, it would be easier to use a stream with 7 numbers and generate the 8th number traditionally. However, if you really want to have one stream with all 8 numbers in it, you can create one by concatenating two streams.
IntStream.concat(
new Random().ints(0, 64).distinct().limit(7).sorted(),
new Random().ints(0, 64)
).distinct().limit(8).forEach(System.out::println);