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.
Java 8 (not that it has advantages):
Grouping size:
In old style:
In new style:
Thanks to @StuartMarks for the forgotten toList.
If you already have or don't mind adding the Guava library, you don't need to reinvent the wheel.
Simply do:
final List<List<String>> splittedList = Lists.partition(bigList, 10);
where
bigList
implements theList
interface and10
is the desired size of each sublist (the last may be smaller)I am also doing key:value mapping for values with index.
Output:
generic function :
enjoy it~ :)
You can use this finalList as it contains the list of chuncks of the oldList.
If you're constrained by PL/SQL
in
limits then you want to know how to split a list into chunks of size <=n, where n is the limit. This is a much simpler problem as it does not require knowing the size of the list in advance.Pseudocode: