private static Iterable < List < Map . Entry < Symbol , File > > > splitToNChunks ( final ImmutableMap < Symbol , File > inputMap , int numChunks ) { checkArgument ( numChunks > 0 ) ; final List < Map . Entry < Symbol , File > > emptyChunk = ImmutableList . of ( ) ; if ( inputMap . isEmpty ( ) ) { return Collections . nCopies ( numChunks , emptyChunk ) ; } final int chunkSize = IntMath . divide ( inputMap . size ( ) , numChunks , RoundingMode . UP ) ; final ImmutableList < List < Map . Entry < Symbol , File > > > chunks = ImmutableList . copyOf ( splitToChunksOfFixedSize ( inputMap , chunkSize ) ) ; if ( chunks . size ( ) == numChunks ) { return chunks ; } else { // there weren't enough elements to make the desired number of chunks, so we need to // pad with empty chunks final int shortage = numChunks - chunks . size ( ) ; final List < List < Map . Entry < Symbol , File > > > padding = Collections . nCopies ( shortage , emptyChunk ) ; return Iterables . concat ( chunks , padding ) ; } }
If there are fewer files than chunks fewer than numChunks will be returned .