There are several SQL statements in my program that contain IN-classes with the given identifiers. The problem is that in some cases there may be more than 1000 identifiers, which causes Oracle to crash with ORA-01795. Too many elements.
So, I want to split this list into several subscriptions.
Example: I have 2403 identifiers
The result will be three lists:
- 0 - 999
- 1000 - 1999
- 2000 - 2402
I wrote a piece of code that works, but looks awful. Is there a better solution to this problem? Maybe something with Collectors and a group or something like that?
My code is:
Map<Integer, List<Long>> result = new HashMap<>();
ArrayList<Long> asList = new ArrayList<Long>(listOfIds);
IntStream.range(0, (listOfIds.size() / 1000) + 1)
.forEach(partGroup -> result.put(partGroup, asList.subList(partGroup * 1000, (partGroup * 1000) + Math.min(1000,
asList.size() - partGroup * 1000))));
source
share