Java List Split splitListIntoParts(List list, int sizeOfEachPart)

Here you can find the source of splitListIntoParts(List list, int sizeOfEachPart)

Description

split List Into Parts

License

Open Source License

Declaration

public static List<List> splitListIntoParts(List list, int sizeOfEachPart) 

Method Source Code


//package com.java2s;
/*/* w ww.  j av a  2 s  .  c  o  m*/
 * Copyright (c) 2005-2011 Grameen Foundation USA
 * All rights reserved.
 *
 * 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.
 *
 * See also http://www.apache.org/licenses/LICENSE-2.0.html for an
 * explanation of the license and how it is applied.
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static List<List> splitListIntoParts(List list, int sizeOfEachPart) {
        if (sizeOfEachPart <= 0) {
            throw new IllegalArgumentException(
                    "Cannot split list into sizes of zero or less. Given value: " + sizeOfEachPart);
        }

        List<List> result = new ArrayList<List>();
        int start = 0, end = sizeOfEachPart;
        while (start < list.size()) {
            result.add(list.subList(start, Math.min(end, list.size())));
            start = end;
            end += sizeOfEachPart;
        }
        return result;
    }
}

Related

  1. splitList(List _list, int _elements)
  2. splitList(List list, int page)
  3. splitList(List parent, int subSize)
  4. splitList(List source, int count)
  5. splitList(List sourceList, int spliTotal)