Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
616 views
in Technique[技术] by (71.8m points)

java - How to split array list into equal parts?

Is there anyway to split ArrayList into different parts without knowing size of it until runtime? I know there is a method called:

list.subList(a,b);

but we need to explicitly mention staring and ending range of list. My problem is, we get a arraylist containing account numbers which is having data like 2000,4000 account numbers (there numbers will not be known during coding time), and I need to pass this acc nos into IN query of PL/SQL, as IN doesn't support more than 1000 values in it, I am trying to split into multiple chunks and sending it to query

Note: I cannot use any external libraries like Guava etc.. :( Any guide in this regard is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This should give you all your parts :

int partitionSize = 1000;
List<List<Integer>> partitions = new LinkedList<List<Integer>>();
for (int i = 0; i < originalList.size(); i += partitionSize) {
    partitions.add(originalList.subList(i,
            Math.min(i + partitionSize, originalList.size())));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...