I have hashmap: Map> myMap
and I want to split it to list that contains Map:
List>> listofMaps;
, and each map will be max 100 keys.
I know how to do it in the regular way..(foreach on the entryset , and every 100 items create new map).
Is there any option to do it with java 8 lambda or something? (something like Lists.partitions() ..) ?
解决方案
Using my unorderedBatches() collector from this answer:
Collector>, ?, List>>> batchesCollector =
unorderedBatches(100,
Collectors.toMap(Entry::getKey, Entry::getValue), Collectors.toList());
List>> listofMaps = myMap.entrySet().stream()
.collect(batchesCollector);