Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package utility;
- import java.util.AbstractList;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.List;
- import java.util.concurrent.atomic.AtomicInteger;
- import java.util.stream.Collectors;
- public final class Partition<T> extends AbstractList<List<T>> {
- private final List<T> list;
- private final int chunkSize;
- public Partition(List<T> list, int chunkSize) {
- this.list = new ArrayList<>(list);
- this.chunkSize = chunkSize;
- }
- public static <T> Partition<T> ofSize(List<T> list, int chunkSize) {
- return new Partition<>(list, chunkSize);
- }
- @Override
- public List<T> get(int index) {
- int start = index * chunkSize;
- int end = Math.min(start + chunkSize, list.size());
- if (start > end) {
- throw new IndexOutOfBoundsException("Index " + index + " is out of the list range <0," + (size() - 1) + ">");
- }
- return new ArrayList<>(list.subList(start, end));
- }
- @Override
- public int size() {
- return (int) Math.ceil((double) list.size() / (double) chunkSize);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement