如何将一个数组列表(size=1000)拆分为多个相同大小(=10)的数组列表?
ArrayList<Integer> results;
如何将一个数组列表(size=1000)拆分为多个相同大小(=10)的数组列表?
ArrayList<Integer> results;
当前回答
Java8流,一个表达式,没有其他库(两个解决方案,无需创建不必要的映射):
List<List<Integer>> partitionedList = IntStream.range(0, (list.size()-1)/targetSize+1)
.mapToObj(i -> list.subList(i*targetSize, Math.min(i*targetSize+targetSize, list.size())))
.collect(Collectors.toList());
List<List<Integer>> partitionedList2 = IntStream.iterate(0, i -> i < list.size(), i -> i + targetSize)
.mapToObj(i -> list.subList(i, Math.min(i + targetSize, list.size())))
.collect(Collectors.toList());
请记住,这些是子列表,因此对原始列表的更改也会影响这些子列表。
如果你不希望它们是子列表,而是新创建的独立列表,可以这样修改:
List<List<Integer>> partitionedList = IntStream.range(0, (list.size()-1)/targetSize+1)
.mapToObj(i -> IntStream.range(i*targetSize, Math.min(i*targetSize+targetSize, list.size())).mapToObj(j -> list.get(j)).collect(Collectors.toList()))
.collect(Collectors.toList());
List<List<Integer>> partitionedList2 = IntStream.iterate(0, i -> i < list.size(), i -> i + targetSize)
.mapToObj(i -> IntStream.range(i, Math.min(i + targetSize, list.size())).mapToObj(j -> list.get(j)).collect(Collectors.toList()))
.collect(Collectors.toList());
其他回答
我猜你遇到的问题是命名100个数组列表并填充它们。您可以创建一个数组列表数组,并使用循环填充每个数组列表。
最简单(也是最愚蠢的)的方法是这样的:
ArrayList results = new ArrayList(1000);
// populate results here
for (int i = 0; i < 1000; i++) {
results.add(i);
}
ArrayList[] resultGroups = new ArrayList[100];
// initialize all your small ArrayList groups
for (int i = 0; i < 100; i++) {
resultGroups[i] = new ArrayList();
}
// put your results into those arrays
for (int i = 0; i < 1000; i++) {
resultGroups[i/10].add(results.get(i));
}
Apache Commons Collections 4在ListUtils类中有一个分区方法。下面是它的工作原理:
import org.apache.commons.collections4.ListUtils;
...
int targetSize = 100;
List<Integer> largeList = ...
List<List<Integer>> output = ListUtils.partition(largeList, targetSize);
import org.apache.commons.collections4.ListUtils;
ArrayList<Integer> mainList = .............;
List<List<Integer>> multipleLists = ListUtils.partition(mainList,100);
int i=1;
for (List<Integer> indexedList : multipleLists){
System.out.println("Values in List "+i);
for (Integer value : indexedList)
System.out.println(value);
i++;
}
您需要知道您划分列表的块大小。假设您有一个包含108个条目的列表,您需要25个块大小。因此,你最终会得到5个列表:
4项各有25项; 有8个元素的。
代码:
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
for (int i=0; i<108; i++){
list.add(i);
}
int size= list.size();
int j=0;
List< List<Integer> > splittedList = new ArrayList<List<Integer>>() ;
List<Integer> tempList = new ArrayList<Integer>();
for(j=0;j<size;j++){
tempList.add(list.get(j));
if((j+1)%25==0){
// chunk of 25 created and clearing tempList
splittedList.add(tempList);
tempList = null;
//intializing it again for new chunk
tempList = new ArrayList<Integer>();
}
}
if(size%25!=0){
//adding the remaining enteries
splittedList.add(tempList);
}
for (int k=0;k<splittedList.size(); k++){
//(k+1) because we started from k=0
System.out.println("Chunk number: "+(k+1)+" has elements = "+splittedList.get(k).size());
}
}
Java8流,一个表达式,没有其他库:
List<String> input = ...
int partitionSize = ...
Collection<List<String>> partitionedList = IntStream.range(0, input.size())
.boxed()
.collect(Collectors.groupingBy(partition -> (partition / partitionSize), Collectors.mapping(elementIndex -> input.get(elementIndex), Collectors.toList())))
.values();
测试:
List<String> input = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h" ,"i");
partitionSize = 1 - > [[a], [b], [c], [d], [e], [f], [g], [h],[我]] partitionSize = 2 - > [[a, b], c, d, e, f, g, h,[我]] partitionSize = 3 - > [a, b, c, d, e, f, g, h,我]] partitionSize = 7 - > [[a, b, c, d, e, f, g], [h,我]] partitionSize = 100 -> [[a, b, c, d, e, f, g, h, i]]