Java List Split split(List list, int size)

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

Description

Splits the list.

License

Apache License

Parameter

Parameter Description
T the type of list element
list the list
size the piece size

Exception

Parameter Description
NullPointerException if the list parameter is null
IllegalArgumentException if the size parameter is less than 1

Return

the split lists.

Declaration

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

Method Source Code


//package com.java2s;
/*//from   ww  w. j ava  2s  .  c  o m
 * Copyright 2004-2010 the Seasar Foundation and the Others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

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

public class Main {
    /**
     * Splits the list.
     * 
     * @param <T>
     *            the type of list element
     * @param list
     *            the list
     * @param size
     *            the piece size
     * @return the split lists.
     * @throws NullPointerException
     *             if the list parameter is null
     * @throws IllegalArgumentException
     *             if the size parameter is less than 1
     */
    public static <T> List<List<T>> split(List<T> list, int size)
            throws NullPointerException, IllegalArgumentException {
        if (list == null) {
            throw new NullPointerException("The list parameter is null.");
        }
        if (size <= 0) {
            throw new IllegalArgumentException("The size parameter must be more than 0.");
        }
        int num = list.size() / size;
        int mod = list.size() % size;
        List<List<T>> ret = new ArrayList<List<T>>(mod > 0 ? num + 1 : num);
        for (int i = 0; i < num; i++) {
            ret.add(list.subList(i * size, (i + 1) * size));
        }
        if (mod > 0) {
            ret.add(list.subList(num * size, list.size()));
        }
        return ret;
    }
}

Related

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