Skip to content

Commit

Permalink
chore: add javadoc for CollectionUtils.chunked and set initialCapacity
Browse files Browse the repository at this point in the history
  • Loading branch information
iProdigy authored and PhilippHeuer committed Jun 2, 2020
1 parent 3f774bf commit b82257e
Showing 1 changed file with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package com.github.twitch4j.common.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class CollectionUtils {

/**
* Assigns elements of the given iterable to chunks not exceeding the desired size
*
* @param iterable the source of elements to be assigned to a chunk
* @param size the maximum size of each chunk
* @return a list of the chunks, or an empty list if the iterable yielded no elements
* @throws NullPointerException if the passed iterable is null
*/
public static <T> List<List<T>> chunked(Iterable<T> iterable, int size) {
List<List<T>> chunks = new ArrayList<>();
List<T> chunk = null;
Iterator<T> it = iterable.iterator();
for (int i = 0; it.hasNext(); i++) {
if (i % size == 0) {
chunk = new ArrayList<>();
chunk = new ArrayList<>(size);
chunks.add(chunk);
}

Expand Down

0 comments on commit b82257e

Please sign in to comment.