Java List Split splitList(List list, int page)

Here you can find the source of splitList(List list, int page)

Description

split List

License

Apache License

Declaration

public static <T> List<List<T>> splitList(List<T> list, int page) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Main {

    public static <T> List<List<T>> splitList(List<T> list, int page) {
        List<List<T>> listArray = new ArrayList<List<T>>();
        Map<Integer, List<T>> maps = new HashMap<Integer, List<T>>();
        int size = list.size();
        for (int l = 0; l < page; l++) {
            maps.put(l, new ArrayList<T>());
        }/*from  www.j a  va 2 s  .c  o  m*/
        if (size % page == 0) {
            for (int k = 0; k < size; k++) {
                maps.get(k % page).add(list.get(k));
            }
        } else {
            for (int i = 0; i < size;) {
                for (int j = 0; j < page; j++) {
                    if (i + page > size) {
                        int left = size - i;
                        for (int m = 0; m < left; m++) {
                            maps.get(m).add(list.get(i));
                            i++;
                        }
                    } else {
                        maps.get(j).add(list.get(i));
                        i++;
                    }
                }
            }
        }

        Iterator<Integer> it = maps.keySet().iterator();
        while (it.hasNext()) {
            listArray.add(maps.get(it.next()));
        }
        return listArray;
    }
}

Related

  1. split(List toSplit, int howOften)
  2. split(List list, int divide)
  3. splitList(List files, int limit)
  4. splitList(List list, int number)
  5. splitList(List _list, int _elements)
  6. splitList(List parent, int subSize)
  7. splitList(List source, int count)
  8. splitList(List sourceList, int spliTotal)
  9. splitListIntoParts(List list, int sizeOfEachPart)