I'm having something like:
List<Data> dataList = stepts.stream()
.flatMap(step -> step.getPartialDataList().stream())
.collect(Collectors.toList());
So I'm combining into dataList multiple lists from every step.
My problem is that dataList might run into OutOfMemoryError. Any suggestions on how I can batch the dataList and save the batches into db?
My primitive idea is to:
for (Step step : steps) {
List<Data> partialDataList = step.getPartialDataList();
if (dataList.size() + partialDataList.size() <= MAXIMUM_SIZE) {
dataList.addAll(partialDataList);
} else {
saveIntoDb(dataList);
dataList = new ArrayList<>();
}
}
PS: I know there is this post, but the difference is that I might not be able to store whole data in memory.
LE: getPartialDataList metod is more like createPartialDataList()