As an example, I want to create an infinite stream of Groups of tens like this:
0=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
1=[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
2=[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
...
I want to use an inifinte stream of ints as an Input, which should then be grouped. If the first stream iterates 10 times the resulting stream should have iterated only once.
My working, but not very elegant code looks like this:
// create a stream from 0 (inclusive) to 100 (exclusive)
IntStream.iterate(0, i -> i+1).boxed().limit(100)
// slow down
.peek((i) -> {try {Thread.sleep(50);} catch (InterruptedException e) {}})
// group by tens
/* ugly: */.collect(Collectors.groupingBy(i -> i / 10)).entrySet()
/* not working: */ //.makeSequentialGroups(i -> i / 10)
// print to console
.forEach(System.out::println);
How do I make groups of an int stream, without having to collect and re-stream? (If possible even without having to use boxing)