Java - Stream - Collect every N elements

后端 未结 6 1275
忘掉有多难
忘掉有多难 2020-12-30 02:38

I am trying to learn java - stream. I am able to do simple iteration / filter / map / collection etc.

When I was kind of trying to collect every 3 elements and print

6条回答
  •  再見小時候
    2020-12-30 03:42

    If you have Guava in your project, you can use Iterables.partition method:

    import com.google.common.collect.Iterables;
    import com.google.common.collect.Streams;
    ...
    
    Stream> stream = Streams.stream(Iterables.partition(list, 3));
    

提交回复
热议问题