Here you can find the source of partitionFixed(int maxNumChunks, Collection
Parameter | Description |
---|---|
maxNumChunks | the maximum number of chunks to return |
coll | the collection to be chunked up |
public static <T> List<List<T>> partitionFixed(int maxNumChunks, Collection<T> coll)
//package com.java2s; /*//from w ww . j a v a 2 s .c o m * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.TreeMap; public class Main { /** * Fills up chunks out of a collection (given a maximum amount of chunks) * * i.e. partitionFixed(5, [1,2,3]) -> [[1,2,3]] * partitionFixed(5, [1..9]) -> [[1,2], [3,4], [5,6], [7,8], [9]] * partitionFixed(3, [1..10]) -> [[1,2,3,4], [5,6,7], [8,9,10]] * @param maxNumChunks the maximum number of chunks to return * @param coll the collection to be chunked up * @return a list of the chunks, which are themselves lists. */ public static <T> List<List<T>> partitionFixed(int maxNumChunks, Collection<T> coll) { List<List<T>> ret = new ArrayList<>(); if (maxNumChunks == 0 || coll == null) { return ret; } Map<Integer, Integer> parts = integerDivided(coll.size(), maxNumChunks); // Keys sorted in descending order List<Integer> sortedKeys = new ArrayList<Integer>(parts.keySet()); Collections.sort(sortedKeys, Collections.reverseOrder()); Iterator<T> it = coll.iterator(); for (Integer chunkSize : sortedKeys) { if (!it.hasNext()) { break; } Integer times = parts.get(chunkSize); for (int i = 0; i < times; i++) { if (!it.hasNext()) { break; } List<T> chunkList = new ArrayList<>(); for (int j = 0; j < chunkSize; j++) { if (!it.hasNext()) { break; } chunkList.add(it.next()); } ret.add(chunkList); } } return ret; } public static TreeMap<Integer, Integer> integerDivided(int sum, int numPieces) { int base = sum / numPieces; int numInc = sum % numPieces; int numBases = numPieces - numInc; TreeMap<Integer, Integer> ret = new TreeMap<Integer, Integer>(); ret.put(base, numBases); if (numInc != 0) { ret.put(base + 1, numInc); } return ret; } public static <S, T> T get(Map<S, T> m, S key, T def) { T ret = m.get(key); if (ret == null) { ret = def; } return ret; } }