I have a List<Long>
elements. The list is huge. The list size would be around 1M records. I am trying to partition the list to get the min and max for each partition, which I will be passing them to the DB queries.
I am currently doing this in the following way:
int gridSize = 6;
List<Long> idList = findAllIds();
List<List<Long>> partitionedList = ListUtils.partition(idList, (idList.size() + gridSize - 1) / gridSize);
for(List<Long> ids : partitionedList) {
LongSummaryStatistics idStats = ids.stream().collect(Collectors.summarizingLong(e -> e));
repo.callToDb(idStats.getMin(), idStats.getMax());
}
The above code works fine. But I am unnecessarily collecting all the keys to a list and then partition and save them (which saves them to a different list) and then looping through them to get the idStats.
Is there a more efficient way to do this instead of creating these many lists and consuming so much of memory?