Split a list of integers into smaller batches of a specified size. The last batch may contain fewer elements if the list size is not evenly divisible by the batch size. This is particularly useful for processing large datasets in chunks to respect Salesforce governor limits.
The method signature is: public static List<List<Integer>> chunkList(List<Integer> inputList, Integer batchSize)
Examples:
Input: inputList = [1, 2, 3, 4, 5], batchSize = 2
Output: [[1, 2], [3, 4], [5]]
Explanation: The list is split into batches of size 2, with the last batch containing the remaining 1 element
Input: inputList = [1, 2, 3, 4, 5, 6], batchSize = 3
Output: [[1, 2, 3], [4, 5, 6]]
Explanation: The list is evenly divisible by the batch size, resulting in 2 complete batches
Input: inputList = [1, 2], batchSize = 5
Output: [[1, 2]]
Explanation: The batch size is larger than the list, so the entire list becomes one batch
Apex Code Editor
Welcome to Lightning Challenge!
Create an Account
Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.
Create accountHow It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay Feb 2026
Contest runs February 1 - 28. Complete challenges to climb the leaderboard!
Points are calculated for challenges completed during the contest period.