【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();
java数组字符串转数组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 = upListByQuantity(allList, 50);
int i = 0;
List<String> list = null;
for (int c = 0; c < groupList.size(); c++) {
list = (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 = upListByQuantity(allList, 50);
int i = 0;
List<String> list = null;
for (int c = 0; c < groupList.size(); c++) {
list = (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 = upListByQuantity(allList, 50);
int i = 0;
List<String> list = null;
for (int c = 0; c < groupList.size(); c++) {
list = (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 = upListByQuantity(null, 50);        System.out.println(groupList);
groupList = upListByQuantity(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 = upListByQuantity(allList, 50);
int i = 0;
List<String> list = null;
for (int c = 0; c < groupList.size(); c++) {
list = (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 = upListByQuantity(allList, 50);        (0).remove(0);
int i = 0;
List<String> list = null;
for (int c = 0; c < groupList.size(); c++) {
list = (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());
/* 按坐标删除 */
/* ⽤迭代器删除 */
/*
Iterator<String> i = subList1.iterator();
<();
*/
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 = upListByQuantity(allList, 50);
int i = 0;
List<String> list = null;
for (int c = 0; c < groupList.size(); c++) {
list = (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 = upListByQuantity(allList, 50);
int i = 0;
List<String> list = null;
for (int c = 0; c < groupList.size(); c++) {
list = (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 = upListByQuantity(allList, 50);
int i = 0;
List<String> list = null;
for (int c = 0; c < groupList.size(); c++) {
list = (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 = upListByQuantity(null, 50);        System.out.println(groupList);
groupList = upListByQuantity(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 = upListByQuantity(allList, 50);
int i = 0;
List<String> list = null;
for (int c = 0; c < groupList.size(); c++) {
list = (c);
System.out.println("第" + (c + 1) + "组: ");
for (String temp : list) {
System.out.print(temp + ", ");
}
System.out.println();
}
}
}
View Code

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。