如何将一个数组列表(size=1000)拆分为多个相同大小(=10)的数组列表?
ArrayList<Integer> results;
如何将一个数组列表(size=1000)拆分为多个相同大小(=10)的数组列表?
ArrayList<Integer> results;
当前回答
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]]
其他回答
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 ], .........
你会在日志里看到的
您需要知道您划分列表的块大小。假设您有一个包含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());
}
}
让我们假设您想要将列表分割为多个块的类作为库类。
所以让我们说这个类被称为“shared”,in应该是final,以确保它不会被扩展。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public final class Shared {
List<Integer> input;
int portion;
public Shared(int portion, Integer... input) {
this.setPortion(portion);
this.setInput(input);
}
public List<List<Integer>> listToChunks() {
List<List<Integer>> result = new ArrayList<List<Integer>>();
int size = this.size();
int startAt = 0;
int endAt = this.portion;
while (endAt <= size) {
result.add(this.input.subList(startAt, endAt));
startAt = endAt;
endAt = (size - endAt < this.portion && size - endAt > 0) ? (this.size()) : (endAt + this.portion);
}
return result;
}
public int size() {
return this.input.size();
}
public void setInput(Integer... input) {
if (input != null && input.length > 0)
this.input = Arrays.asList(input);
else
System.out.println("Error 001 : please enter a valid array of integers.");
}
public void setPortion(int portion) {
if (portion > 0)
this.portion = portion;
else
System.out.println("Error 002 : please enter a valid positive number.");
}
}
接下来,让我们尝试从另一个持有公共静态void main(String…args)
public class exercise {
public static void main(String[] args) {
Integer[] numbers = {1, 2, 3, 4, 5, 6, 7};
int portion = 2;
Shared share = new Shared(portion, numbers);
System.out.println(share.listToChunks());
}
}
现在,如果输入一个整数数组[1,2,3,4,5,6,7],分区为2。 结果将是[[1,2],[3,4],[5,6],[7]]
使用StreamEx库,您可以使用StreamEx。ofSubLists(List<T> source, int length)方法:
返回一个新的StreamEx,它由给定源列表的不重叠子列表组成,具有指定的长度(最后一个子列表可能更短)。
// Assuming you don't actually care that the lists are of type ArrayList
List<List<Integer>> sublists = StreamEx.ofSubLists(result, 10).toList();
// If you actually want them to be of type ArrayList, per your question
List<List<Integer>> sublists = StreamEx.ofSubLists(result, 10).toCollection(ArrayList::new);
只是要明确一点,这还需要更多的测试…
public class Splitter {
public static <T> List<List<T>> splitList(List<T> listTobeSplit, int size) {
List<List<T>> sublists= new LinkedList<>();
if(listTobeSplit.size()>size) {
int counter=0;
boolean lastListadded=false;
List<T> subList=new LinkedList<>();
for(T t: listTobeSplit) {
if (counter==0) {
subList =new LinkedList<>();
subList.add(t);
counter++;
lastListadded=false;
}
else if(counter>0 && counter<size-1) {
subList.add(t);
counter++;
}
else {
lastListadded=true;
subList.add(t);
sublists.add(subList);
counter=0;
}
}
if(lastListadded==false)
sublists.add(subList);
}
else {
sublists.add(listTobeSplit);
}
log.debug("sublists: "+sublists);
return sublists;
}
}