有一种简单的方法来做到以流下列:分组整数表向分区
public static void main(String[] args) {
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
System.out.print(partitioningValues(integerList, 3));
}
private static Map<Integer, List<Integer>> partitioningValues(List<Integer> integerList, int numberOfPartitions) {
Map<Integer, List<Integer>> integerListMap = new HashMap<>();
BigDecimal limit = BigDecimal.valueOf(integerList.size()/(double) numberOfPartitions);
int limitRounded = limit.setScale(0, BigDecimal.ROUND_UP).intValue();
for (int i = 0; i < numberOfPartitions; i++) {
int toIndex = ((i + 1) * limitRounded) > integerList.size() ? integerList.size() : (i + 1) * limitRounded;
integerListMap.put(i, integerList.subList(i * limitRounded, toIndex));
}
return integerListMap;
}
结果:
{0 = [1,2,3,4] ,1 = [5,6,7,8],2 = [9,10]}
第一溶液不拆分原始列表。 – user6904265
是的,错过了'map' – Saravana
对不起,我的朋友,但没有一个代表一个解决方案..尝试与另一个输入:'列表 integerList = Arrays.asList(10,22,23,4,25,6,27,8 ,9,10);' –
user6904265