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

前端 未结 7 2038
青春惊慌失措
青春惊慌失措 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 00:52

    Using library StreamEx solution would look like

    Stream stream = IntStream.iterate(0, i -> i + 1).boxed().limit(15);
    AtomicInteger counter = new AtomicInteger(0);
    int chunkSize = 4;
    
    StreamEx.of(stream)
            .groupRuns((prev, next) -> counter.incrementAndGet() % chunkSize != 0)
            .forEach(chunk -> System.out.println(chunk));
    

    Output:

    [0, 1, 2, 3]
    [4, 5, 6, 7]
    [8, 9, 10, 11]
    [12, 13, 14]
    

    groupRuns accepts predicate that decides whether 2 elements should be in the same group.

    It produces a group as soon as it finds first element that does not belong to it.

提交回复
热议问题