【Java】List集合按数量分组

有时候,我们需要将大的集合按指定的数量分割成若干个小集合。(比如:集合作为SQL中IN的参数,而SQL又有长度限制,所以需要分批分几次进行查询)

虽然此需求感觉不常见,但偶也写过几次类似的方法,故记录之。

更新于2017年:其实Guava库有个已有的方法实现此需求:Lists.partition(List<T> list, int size)

 

v2,更新于2016-01-20

v1的代码使用后发现有问题,如果对分组后的一子集作删除操作,其他子集用迭代器遍历时会出现ConcurrentModificationException。

修改后的代码如下:

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(new ArrayList(list.subList(count, (count + quantity) > list.size() ? list.size() : count + quantity)));
            count += quantity;
        }
        
        return wrapList;
    }
    
}
View Code

 

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();
        }
    }
    

    /**
     * 出现ConcurrentModificationException的情况
     */
    @Test
    public void test6() {
        List<String> allList = new ArrayList<String>();
        for (int i = 1; i <= 55; i++) {
            allList.add(i  + "");
        }
        
        List<List<String>> groupList = CollectionGroupUtil.groupListByQuantity(allList, 50);
        groupList.get(0).remove(0);
        
        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();
        }
    }
    
}
View Code

 

以简单的方式重现ConcurrentModificationException异常:

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


public class SeeConcurrentModificationExceptionInSubList {

    public static void main(String[] args) {
        List<String> originalList = new ArrayList<String>();
        for (int i = 1; i <= 5; i++) {
            originalList.add(i + "");
        }
        
        List<String> subList1 = originalList.subList(0, 3);
        List<String> subList2 = originalList.subList(3, originalList.size());
        
        /* 按坐标删除 */
        subList1.remove(0);
        
        /* 用迭代器删除 */
        /*
        Iterator<String> i = subList1.iterator();
        i.next();
        i.remove();
        */
        
        System.out.println("subList1 -> ");
        for (String tempStr : subList1) {
            System.out.println(tempStr);
        }
        
        System.out.println("subList2 -> ");
        for (String tempStr : subList2) {
            System.out.println(tempStr);
        }

    }

}
View Code

 

v1,更新于2016-01-18

工具类

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;
    }
    
}
View Code

 

测试类

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();
        }
    }
    
}
View Code

 

转载于:https://www.cnblogs.com/nick-huang/p/5139771.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值