如何将一个数组列表(size=1000)拆分为多个相同大小(=10)的数组列表?
ArrayList<Integer> results;
如何将一个数组列表(size=1000)拆分为多个相同大小(=10)的数组列表?
ArrayList<Integer> results;
当前回答
private ArrayList<List<String>> chunkArrayList(ArrayList<String> arrayToChunk, int chunkSize) {
ArrayList<List<String>> chunkList = new ArrayList<>();
int guide = arrayToChunk.size();
int index = 0;
int tale = chunkSize;
while (tale < arrayToChunk.size()){
chunkList.add(arrayToChunk.subList(index, tale));
guide = guide - chunkSize;
index = index + chunkSize;
tale = tale + chunkSize;
}
if (guide >0) {
chunkList.add(arrayToChunk.subList(index, index + guide));
}
Log.i("Chunked Array: " , chunkList.toString());
return chunkList;
}
例子
ArrayList<String> test = new ArrayList<>();
for (int i=1; i<=1000; i++){
test.add(String.valueOf(i));
}
chunkArrayList(test,10);
输出
分块:[[1,2,3,4,5,6,7,8,9,10),(11、12、13、14、15、16、17、18、19、20],[21日,22日,23日,24日,25日,26日,27日,28日,29日,30日],[第三十一条、第三十二条、第三十三,34岁,35岁,36岁,37岁,38岁,39岁,40],[41、42、43、44、45、46岁,47岁,48岁,49岁,50],[51岁,52岁,53岁,54岁,55岁,56岁,57岁的58岁的59岁60],[61,62,63,64,65,66,67,68,69,70],[71,72,73,74,75,76,77,78,79,80],[81,82,83,84,85,86,87,88,89,90],[91,92,93,94,95,96,97,98,99,100 ], .........
你会在日志里看到的
其他回答
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());
你可以使用subList(int fromIndex, int toIndex)来获取原始列表的一部分。
来自API:
返回该列表中指定的fromIndex(包含)和toIndex(不包含)之间部分的视图。(如果fromIndex和toIndex相等,返回的列表为空。)返回的列表由该列表支持,因此返回列表中的非结构性更改会反映在该列表中,反之亦然。返回的列表支持该列表支持的所有可选列表操作。
例子:
List<Integer> numbers = new ArrayList<Integer>(
Arrays.asList(5,3,1,2,9,5,0,7)
);
List<Integer> head = numbers.subList(0, 4);
List<Integer> tail = numbers.subList(4, 8);
System.out.println(head); // prints "[5, 3, 1, 2]"
System.out.println(tail); // prints "[9, 5, 0, 7]"
Collections.sort(head);
System.out.println(numbers); // prints "[1, 2, 3, 5, 9, 5, 0, 7]"
tail.add(-1);
System.out.println(numbers); // prints "[1, 2, 3, 5, 9, 5, 0, 7, -1]"
如果您需要这些切碎的列表不是一个视图,那么只需从subblist创建一个新的List。下面是一个把这些东西放在一起的例子:
// chops a list into non-view sublists of length L
static <T> List<List<T>> chopped(List<T> list, final int L) {
List<List<T>> parts = new ArrayList<List<T>>();
final int N = list.size();
for (int i = 0; i < N; i += L) {
parts.add(new ArrayList<T>(
list.subList(i, Math.min(N, i + L)))
);
}
return parts;
}
List<Integer> numbers = Collections.unmodifiableList(
Arrays.asList(5,3,1,2,9,5,0,7)
);
List<List<Integer>> parts = chopped(numbers, 3);
System.out.println(parts); // prints "[[5, 3, 1], [2, 9, 5], [0, 7]]"
parts.get(0).add(-1);
System.out.println(parts); // prints "[[5, 3, 1, -1], [2, 9, 5], [0, 7]]"
System.out.println(numbers); // prints "[5, 3, 1, 2, 9, 5, 0, 7]" (unmodified!)
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]]
**Divide a list to lists of n size**
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
public final class PartitionUtil<T> extends AbstractList<List<T>> {
private final List<T> list;
private final int chunkSize;
private PartitionUtil(List<T> list, int chunkSize) {
this.list = new ArrayList<>(list);
this.chunkSize = chunkSize;
}
public static <T> PartitionUtil<T> ofSize(List<T> list, int chunkSize) {
return new PartitionUtil<>(list, chunkSize);
}
@Override
public List<T> get(int index) {
int start = index * chunkSize;
int end = Math.min(start + chunkSize, list.size());
if (start > end) {
throw new IndexOutOfBoundsException("Index " + index + " is out of the list range <0," + (size() - 1) + ">");
}
return new ArrayList<>(list.subList(start, end));
}
@Override
public int size() {
return (int) Math.ceil((double) list.size() / (double) chunkSize);
}
}
Function call :
List<List<String>> containerNumChunks = PartitionUtil.ofSize(list, 999)
详情:https://e.printstacktrace.blog/divide-a-list-to-lists-of-n-size-in-Java-8/
这对我很有用
/**
* Returns List of the List argument passed to this function with size = chunkSize
*
* @param largeList input list to be portioned
* @param chunkSize maximum size of each partition
* @param <T> Generic type of the List
* @return A list of Lists which is portioned from the original list
*/
public static <T> List<List<T>> chunkList(List<T> list, int chunkSize) {
if (chunkSize <= 0) {
throw new IllegalArgumentException("Invalid chunk size: " + chunkSize);
}
List<List<T>> chunkList = new ArrayList<>(list.size() / chunkSize);
for (int i = 0; i < list.size(); i += chunkSize) {
chunkList.add(list.subList(i, i + chunkSize >= list.size() ? list.size()-1 : i + chunkSize));
}
return chunkList;
}
例如:
List<Integer> stringList = new ArrayList<>();
stringList.add(0);
stringList.add(1);
stringList.add(2);
stringList.add(3);
stringList.add(4);
stringList.add(5);
stringList.add(6);
stringList.add(7);
stringList.add(8);
stringList.add(9);
List<List<Integer>> chunkList = getChunkList1(stringList, 2);