如何将一个数组列表(size=1000)拆分为多个相同大小(=10)的数组列表?
ArrayList<Integer> results;
如何将一个数组列表(size=1000)拆分为多个相同大小(=10)的数组列表?
ArrayList<Integer> results;
当前回答
创建一个新列表,并使用addAll()方法添加源列表的子列表视图以创建新的子列表
List<T> newList = new ArrayList<T>();
newList.addAll(sourceList.subList(startIndex, endIndex));
其他回答
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());
List<List<Integer>> allChunkLists = new ArrayList<List<Integer>>();
List<Integer> chunkList = null;
int fromIndex = 0;
int toIndex = CHUNK_SIZE;
while (fromIndex < origList.size()) {
chunkList = origList.subList(fromIndex, (toIndex > origList.size() ? origList.size() : toIndex));
allChunkLists.add(chunkList);
fromIndex = toIndex;
toIndex += CHUNK_SIZE;
}
没有库,只有Java的subList()。toIndex需要适当地有界,以避免在subList()中出现越界错误。
如果你不想导入apache Commons库,试试下面这段简单的代码:
final static int MAX_ELEMENT = 20;
public static void main(final String[] args) {
final List<String> list = new ArrayList<String>();
for (int i = 1; i <= 161; i++) {
list.add(String.valueOf(i));
System.out.print("," + String.valueOf(i));
}
System.out.println("");
System.out.println("### >>> ");
final List<List<String>> result = splitList(list, MAX_ELEMENT);
for (final List<String> entry : result) {
System.out.println("------------------------");
for (final String elm : entry) {
System.out.println(elm);
}
System.out.println("------------------------");
}
}
private static List<List<String>> splitList(final List<String> list, final int maxElement) {
final List<List<String>> result = new ArrayList<List<String>>();
final int div = list.size() / maxElement;
System.out.println(div);
for (int i = 0; i <= div; i++) {
final int startIndex = i * maxElement;
if (startIndex >= list.size()) {
return result;
}
final int endIndex = (i + 1) * maxElement;
if (endIndex < list.size()) {
result.add(list.subList(startIndex, endIndex));
} else {
result.add(list.subList(startIndex, list.size()));
}
}
return result;
}
我猜你遇到的问题是命名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));
}
Java 8
我们可以根据大小或条件拆分列表。
static Collection<List<Integer>> partitionIntegerListBasedOnSize(List<Integer> inputList, int size) {
return inputList.stream()
.collect(Collectors.groupingBy(s -> (s-1)/size))
.values();
}
static <T> Collection<List<T>> partitionBasedOnSize(List<T> inputList, int size) {
final AtomicInteger counter = new AtomicInteger(0);
return inputList.stream()
.collect(Collectors.groupingBy(s -> counter.getAndIncrement()/size))
.values();
}
static <T> Collection<List<T>> partitionBasedOnCondition(List<T> inputList, Predicate<T> condition) {
return inputList.stream().collect(Collectors.partitioningBy(s-> (condition.test(s)))).values();
}
然后我们可以把它们用作:
final List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
System.out.println(partitionIntegerListBasedOnSize(list, 4)); // [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10]]
System.out.println(partitionBasedOnSize(list, 4)); // [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10]]
System.out.println(partitionBasedOnSize(list, 3)); // [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
System.out.println(partitionBasedOnCondition(list, i -> i<6)); // [[6, 7, 8, 9, 10], [1, 2, 3, 4, 5]]