Java List Split split(List list, int size)

Here you can find the source of split(List list, int size)

Description

split

License

Open Source License

Declaration

public static <T> List<List<T>> split(List<T> list, int size) 

Method Source Code


//package com.java2s;
import java.util.*;

public class Main {
    public static <T> List<List<T>> split(List<T> list, int size) {
        List<List<T>> result = new ArrayList<>();

        int n = list.size() / size;
        int r = list.size() % size;

        for (int i = 0; i < n; i++) {
            List<T> split = new ArrayList<>();
            for (int j = i * size; j < i * size + size; j++) {
                split.add(list.get(j));//from  www . j  a  v  a 2 s.c  o  m
            }
            result.add(split);
        }

        if (r > 0) {
            List<T> rest = new ArrayList<>();
            for (int i = n * size; i < list.size(); i++) {
                rest.add(list.get(i));
            }
            result.add(rest);
        }

        return result;
    }
}

Related

  1. split(List list)
  2. split(List list, String separator)
  3. split(List list, String separator)
  4. split(List list)
  5. split(List list, final int parts)
  6. split(List list, int size)
  7. split(List list, int size)
  8. split(List list, int size)
  9. split(List list, int splitSize)