Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split hashmap to partitions in java 8 [duplicate]

I have hashmap: Map<String, Set<String>> myMap

and I want to split it to list that contains Map:

List<Map<String,Set<String>>> 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() ..) ?

like image 672
Tal Eden Avatar asked Jan 06 '23 23:01

Tal Eden


1 Answers

Using my unorderedBatches() collector from this answer:

Collector<Entry<String, Set<String>>, ?, List<Map<String, Set<String>>>> batchesCollector = 
    unorderedBatches(100, 
        Collectors.toMap(Entry::getKey, Entry::getValue), Collectors.toList());
List<Map<String, Set<String>>> listofMaps = myMap.entrySet().stream()
        .collect(batchesCollector);
like image 167
Tagir Valeev Avatar answered Jan 18 '23 15:01

Tagir Valeev