Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 400
Pradeep Simha Avatar asked Dec 03 '12 06:12

Pradeep Simha


People also ask

How do you divide an array into 4 equal parts?

Given an array of n non-negative integers. Choose three indices i.e. (0 <= index_1 <= index_ 2<= index_3 <= n) from the array to make four subsets such that the term sum(0, index_1) – sum(index_1, index_2) + sum(index_2, index_3) – sum(index_3, n) is maximum possible.

How do you divide a list into N parts?

Method 1: Break a list into chunks of size N in Python using yield keyword. The yield keyword enables a function to come back where it left off when it is called again. This is the critical difference from a regular function.


1 Answers

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()))); } 
like image 133
Subodh Avatar answered Oct 04 '22 03:10

Subodh