java list 分组数量_【Java】List集合按数量分组

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

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

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

v2,更新于2016-01-20

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

修改后的代码如下:

e971e5c7daccc06644fd4a0c681e3927.png

1aaaca0b4aaa1b01c9ced29305a1c295.png

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

8dce824c6f3f0f13ec65958c1968e719.png

3984793f56aca7b79897169bdc2a533c.png

import java.util.ArrayList;

import java.util.List;

import org.junit.Test;

public class CollectionGroupUtilTest {

/**

* 大于分组数量的情况

*/

@Test

public void test() {

List allList = new ArrayList();

for (int i = 1; i <= 504; i++) {

allList.add(i + "");

}

List> groupList = CollectionGroupUtil.groupListByQuantity(allList, 50);

int i = 0;

List 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 allList = new ArrayList();

for (int i = 1; i <= 45; i++) {

allList.add(i + "");

}

List> groupList = CollectionGroupUtil.groupListByQuantity(allList, 50);

int i = 0;

List 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 allList = new ArrayList();

for (int i = 1; i <= 1; i++) {

allList.add(i + "");

}

List> groupList = CollectionGroupUtil.groupListByQuantity(allList, 50);

int i = 0;

List 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> groupList = CollectionGroupUtil.groupListByQuantity(null, 50);

System.out.println(groupList);

groupList = CollectionGroupUtil.groupListByQuantity(new ArrayList(), 50);

System.out.println(groupList);

}

/**

* 集合刚满一个分组的情况

*/

@Test

public void test5() {

List allList = new ArrayList();

for (int i = 1; i <= 50; i++) {

allList.add(i + "");

}

List> groupList = CollectionGroupUtil.groupListByQuantity(allList, 50);

int i = 0;

List 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 allList = new ArrayList();

for (int i = 1; i <= 55; i++) {

allList.add(i + "");

}

List> groupList = CollectionGroupUtil.groupListByQuantity(allList, 50);

groupList.get(0).remove(0);

int i = 0;

List 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异常:

5173566132cc711a81c5579a87dfc139.png

e3daff546210d7628469e8254dad9931.png

import java.util.ArrayList;

import java.util.List;

public class SeeConcurrentModificationExceptionInSubList {

public static void main(String[] args) {

List originalList = new ArrayList();

for (int i = 1; i <= 5; i++) {

originalList.add(i + "");

}

List subList1 = originalList.subList(0, 3);

List subList2 = originalList.subList(3, originalList.size());

/* 按坐标删除 */

subList1.remove(0);

/* 用迭代器删除 */

/*

Iterator 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

工具类

4880f34ae1b886c21d27d1fb7f95ec01.png

e49fc62198211bd09c1b51115f7aad28.png

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

测试类

f105d7a6b0f18d217acd0271e86ace47.png

c1b70714fed7068e755ae4134860e00d.png

import java.util.ArrayList;

import java.util.List;

import org.junit.Test;

public class CollectionGroupUtilTest {

/**

* 大于分组数量的情况

*/

@Test

public void test() {

List allList = new ArrayList();

for (int i = 1; i <= 504; i++) {

allList.add(i + "");

}

List> groupList = CollectionGroupUtil.groupListByQuantity(allList, 50);

int i = 0;

List 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 allList = new ArrayList();

for (int i = 1; i <= 45; i++) {

allList.add(i + "");

}

List> groupList = CollectionGroupUtil.groupListByQuantity(allList, 50);

int i = 0;

List 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 allList = new ArrayList();

for (int i = 1; i <= 1; i++) {

allList.add(i + "");

}

List> groupList = CollectionGroupUtil.groupListByQuantity(allList, 50);

int i = 0;

List 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> groupList = CollectionGroupUtil.groupListByQuantity(null, 50);

System.out.println(groupList);

groupList = CollectionGroupUtil.groupListByQuantity(new ArrayList(), 50);

System.out.println(groupList);

}

/**

* 集合刚满一个分组的情况

*/

@Test

public void test5() {

List allList = new ArrayList();

for (int i = 1; i <= 50; i++) {

allList.add(i + "");

}

List> groupList = CollectionGroupUtil.groupListByQuantity(allList, 50);

int i = 0;

List 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/tag/Java/default.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值