集册 Java实例教程 拆分Iterable。

拆分Iterable。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

590
拆分Iterable。

/*

 * 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;/* 来 自 N o w J a v a . c o m*/

import java.util.List;


public class Main{

    /**

     * Splits the {@link Iterable}.

     * 

     * @param <T>

     *            the type of {@link Iterable} element

     * @param list

     *            the {@link Iterable}

     * @param size

     *            the piece size

     * @return the split {@link Iterable}s.

     * @throws NullPointerException

     *             if the list parameter is null

     * @throws IllegalArgumentException

     *             if the size parameter is less than 1

     */

    @SuppressWarnings("unchecked")

    public static <T> List<Iterable<T>> split(Iterable<T> list, int size)

            throws NullPointerException, IllegalArgumentException {

        if (list == null) {

            throw new NullPointerException("The list parameter is null.");

        }

        if (size <= 0) {

            throw new IllegalArgumentException(/*from n o w j a   v  a . c o m - 时  代  Java*/

                    "The size parameter must be more than 0.");

        }

        if (list instanceof List<?>) {

            return ListUtil.split((List) list, size);

        }

        List<Iterable<T>> ret = new ArrayList<Iterable<T>>();

        List<T> l = 
展开阅读全文