There are some SQL statements in my program that contain IN-clauses with given Ids. The problem is that in some cases there might be more than 1000 Ids which causes Oracle to crash with ORA-01795. Too many items.
So I want to divide this list into multiple sub-lists.
Example: I have 2403 Ids
Result would be three lists:
- 0 - 999
- 1000 - 1999
- 2000 - 2402
I have written a piece of code that works, but looks terrible. Is there any better solution for this problem? Maybe something with Collectors & groupingby or anything like that?
My code:
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))));