Java List Split split(List list, int divide)

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

Description

split

License

Open Source License

Declaration

public static <V> ArrayList<List<V>> split(List<V> list, int divide) 

Method Source Code


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

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static <V> ArrayList<List<V>> split(List<V> list, int divide) {
        int remain = list.size() % divide;
        int size = (list.size() - remain) / divide;

        ArrayList<List<V>> toReturn = new ArrayList<List<V>>();

        for (int i = 0; i < divide; i++) {
            toReturn.add(i, new ArrayList<V>());
        }//  w w w. j  av a 2 s. c o m

        for (List<V> iterSet : toReturn) {
            List<V> removed = new ArrayList<V>();

            int toAdd = size;

            if (remain > 0) {
                remain--;
                toAdd++;
            }

            for (V obj : list) {
                if (toAdd == 0) {
                    break;
                }

                iterSet.add(obj);
                removed.add(obj);
                toAdd--;
            }

            for (V obj : removed) {
                list.remove(obj);
            }
        }

        return toReturn;
    }
}

Related

  1. split(List list, int size)
  2. split(List list, int size)
  3. split(List list, int splitSize)
  4. split(List lst)
  5. split(List toSplit, int howOften)
  6. splitList(List files, int limit)
  7. splitList(List list, int number)
  8. splitList(List _list, int _elements)
  9. splitList(List list, int page)