2

I'm wondering if there is way with Java-Streams to group elements sequentially.

Let's say I'll have a simple IntStream from 1 to 1000:

IntStream streamTo1000 = IntStream.range(0, 1000);

Can I produce/convert this stream into a stream, that always groups together the next n-elements?

E.g., if I would like to collect always 3 elements together and process them further as one single item? So, that I receive from the streamTo1000 a Stream that looks like

Stream.of(Arrays.asList(1,2,3), Arrays.asList(4,5,6), Arrays.asList(7,8,9), ...

Actually, it would be something like a reverse flatmap.

Could this be done with the available collect, reduce, grouping functions and/or can/should it be done with Stream.iterate?

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
Hansjoerg Wingeier
  • 4,274
  • 4
  • 17
  • 25
  • What you want is to partition the stream, in this case with size 3. Refer to the linke question. – Tunaki May 01 '16 at 11:04
  • This question may as well help: http://stackoverflow.com/questions/32162478/how-can-i-convert-a-stream-of-strings-to-stream-of-string-pairs/32170425 – Helder Pereira May 01 '16 at 17:57

0 Answers0