Java List Split split(List list)

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

Description

split

License

Open Source License

Declaration

static <T> List<List<T>> split(List<T> list) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.List;

public class Main {
    static <T> List<List<T>> split(List<T> list) throws Exception {
        if (list.size() < 2) {
            throw new Exception("Not splittable list");
        }//from  w  ww  .j av  a 2 s .  co m

        int halfSize = list.size() % 2 == 0 ? list.size() / 2 : list.size() / 2 + 1;

        return chopped(list, halfSize);
    }

    static <T> List<List<T>> chopped(List<T> list, final int L) {
        List<List<T>> parts = new ArrayList<List<T>>();
        final int N = list.size();
        for (int i = 0; i < N; i += L) {
            parts.add(new ArrayList<T>(list.subList(i, Math.min(N, i + L))));
        }
        return parts;
    }
}

Related

  1. split(final String line, final char delimiter, final List parts)
  2. split(List data, int max)
  3. split(List list)
  4. split(List list, String separator)
  5. split(List list, String separator)
  6. split(List list, final int parts)
  7. split(List list, int size)
  8. split(List list, int size)
  9. split(List list, int size)