有时候,我们需要将大的集合按指定的数量分割成若干个小集合。(比如:集合作为SQL中IN的参数,而SQL又有长度限制,所以需要分批分几次进行查询)
虽然此需求感觉不常见,但偶也写过几次类似的方法,故记录之。
更新于2017年:其实Guava库有个已有的方法实现此需求:Lists.partition(Listlist, 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 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异常:
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
工具类
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 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