Java List Sub List splitList(List list, int subListNumber)

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

Description

Methode permettant de splitter une liste en un certain nombre de sous-listes

License

Apache License

Parameter

Parameter Description
list Liste originale
subListNumber Nombre de sous-listes souhaitees

Return

Liste de sous-liste splittee

Declaration

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

Method Source Code


//package com.java2s;
/*// w w w .  j  av a  2 s.  c om
 * #%L
 * DROOLS SERVER
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2013 - 2014 Leadware
 * %%
 * 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.
 * #L%
 */

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

public class Main {
    /**
     * Methode permettant de splitter une liste en un certain nombre de sous-listes
     * @param list   Liste originale
     * @param subListNumber   Nombre de sous-listes souhaitees
     * @return   Liste de sous-liste splittee
     */
    public static <T> List<List<T>> splitList(List<T> list, int subListNumber) {

        // Liste a retourner
        List<List<T>> splitted = new ArrayList<List<T>>();

        // Si la liste est vide
        if (list == null || list.size() == 0)
            return splitted;

        // Taille de la liste
        int listSize = list.size();

        // Nombre d'elements de la liste
        int subSize = Math.max(listSize / (subListNumber - 1), 1);

        // Parcours de la liste a splitter
        for (int i = 0; i < listSize; i += subSize) {

            // Ajout de la sous-liste
            splitted.add(new ArrayList<T>(list.subList(i, Math.min(listSize, i + subSize))));
        }

        // On retourne la liste des sous-listes
        return splitted;
    }
}

Related

  1. isSubList(List l1, List l)
  2. lastIndexOfSubList(final List list0, final List list1)
  3. slice(List stringList, int subListSize)
  4. split(List from, int subListSize)
  5. splitIntoSubListsByNumber(final List list, final int numberOfSublists)
  6. splitListToSubLists(List parentList, int subListSize)
  7. startsWith(final List list, final List subList)
  8. sub(List l, int index)
  9. subArray(List listA, List listB)