将list分成等数量

Posted 胡金水

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将list分成等数量相关的知识,希望对你有一定的参考价值。

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

public class CollectionGroupUtil {
    
    public static List groupListByQuantity(List list, int quantity) {
        if (list == null || list.size() == 0) {
            return list;
        }
        
        if (quantity <= 0) {
            new IllegalArgumentException("Wrong quantity.");
        }
        
        List wrapList = new ArrayList();
        int count = 0;
        while (count < list.size()) {
            wrapList.add(list.subList(count, (count + quantity) > list.size() ? list.size() : count + quantity));
            count += quantity;
        }
        
        return wrapList;
    }
    
}

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

import org.junit.Test;


public class CollectionGroupUtilTest {
    
    /**
     * 大于分组数量的情况
     */
    @Test
    public void test() {
        List<String> allList = new ArrayList<String>();
        for (int i = 1; i <= 504; i++) {
            allList.add(i  + "");
        }
        
        List<List<String>> groupList = CollectionGroupUtil.groupListByQuantity(allList, 50);
        int i = 0;
        List<String> list = null;
        for (int c = 0; c < groupList.size(); c++) {
            list = groupList.get(c);
            
            System.out.println("第" + (c + 1) + "组: ");
            for (String temp : list) {
                System.out.print(temp + ", ");
            }
            System.out.println();
        }
    }

    /**
     * 小于分组数量的情况
     */
    @Test
    public void test2() {
        List<String> allList = new ArrayList<String>();
        for (int i = 1; i <= 45; i++) {
            allList.add(i  + "");
        }
        
        List<List<String>> groupList = CollectionGroupUtil.groupListByQuantity(allList, 50);
        int i = 0;
        List<String> list = null;
        for (int c = 0; c < groupList.size(); c++) {
            list = groupList.get(c);
            
            System.out.println("第" + (c + 1) + "组: ");
            for (String temp : list) {
                System.out.print(temp + ", ");
            }
            System.out.println();
        }
    }
    
    /**
     * 集合只有一个记录的情况
     */
    @Test
    public void test3() {
        List<String> allList = new ArrayList<String>();
        for (int i = 1; i <= 1; i++) {
            allList.add(i  + "");
        }
        
        List<List<String>> groupList = CollectionGroupUtil.groupListByQuantity(allList, 50);
        int i = 0;
        List<String> list = null;
        for (int c = 0; c < groupList.size(); c++) {
            list = groupList.get(c);
            
            System.out.println("第" + (c + 1) + "组: ");
            for (String temp : list) {
                System.out.print(temp + ", ");
            }
            System.out.println();
        }
    }
    
    /**
     * 空集合的情况
     */
    @Test
    public void test4() {
        List<List<String>> groupList = CollectionGroupUtil.groupListByQuantity(null, 50);
        System.out.println(groupList);
        
        groupList = CollectionGroupUtil.groupListByQuantity(new ArrayList(), 50);
        System.out.println(groupList);
    }
    
    /**
     * 集合刚满一个分组的情况
     */
    @Test
    public void test5() {
        List<String> allList = new ArrayList<String>();
        for (int i = 1; i <= 50; i++) {
            allList.add(i  + "");
        }
        
        List<List<String>> groupList = CollectionGroupUtil.groupListByQuantity(allList, 50);
        int i = 0;
        List<String> list = null;
        for (int c = 0; c < groupList.size(); c++) {
            list = groupList.get(c);
            
            System.out.println("第" + (c + 1) + "组: ");
            for (String temp : list) {
                System.out.print(temp + ", ");
            }
            System.out.println();
        }
    }
    
}

以上是关于将list分成等数量的主要内容,如果未能解决你的问题,请参考以下文章

java 将一个有大量数据的list集合分成指定大小的list集合

片段中的 super.onCreateView

什么是在 C++ 中获取总内核数量的跨平台代码片段? [复制]

为啥尽管源代码没有变化,但从一个系统到另一个系统的片段数量却有很大差异?

sql将字符串拆分为始终相同数量的片段

将一个list均分成n个list