Is there an elegant way to process a stream in chunks?

前端 未结 7 2025
青春惊慌失措
青春惊慌失措 2020-12-08 00:26

My exact scenario is inserting data to database in batches, so I want to accumulate DOM objects then every 1000, flush them.

I implemented it by putting code in the

相关标签:
7条回答
  • 2020-12-08 01:03

    If you have guava dependency on your project you could do this:

    StreamSupport.stream(Iterables.partition(simpleList, 1000).spliterator(), false).forEach(...);
    

    See https://google.github.io/guava/releases/23.0/api/docs/com/google/common/collect/Lists.html#partition-java.util.List-int-

    0 讨论(0)
提交回复
热议问题