package com.suning.nbillingan.service.billparse;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import com.google.common.collect.Lists;
public class ListsPartition {
public static void main(String[] args) {
//step.1 集合切割正常邏輯
List<Integer> numList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
List<List<Integer>> partList = Lists.partition(numList, 3);
if (!CollectionUtils.isEmpty(partList)) {
for (List<Integer> list : partList) {
System.out.println(list.toString());
}
}
//step.2 切割數量大于集合數量
List<Integer> numList2 = Lists.newArrayList(1,2,3,4,5,6,7,8,9);
List<List<Integer>> partList2 = Lists.partition(numList2, 3);
if (!CollectionUtils.isEmpty(partList2)) {
int i=0;
for (List<Integer> list : partList2) {
System.out.println(list.toString());
System.out.println(i++);
}
numList2.clear();
}
//step.3 修改切割後的集合,檢查原集合是否被修改
List<Integer> numList3 = Lists.newArrayList(1,2,3,4,5,6,7,89,9);
List<List<Integer>> partList3 = Lists.partition(numList3, 3);
if (!CollectionUtils.isEmpty(partList3)) {
for (List<Integer> list : partList3) {
for(int i=0,len = list.size();i<len;i++){
list.set(i,8);
}
}
//列印原集合
System.out.println(numList3.toString());
}
//注意:partition傳回的是原list的subview.視圖,即原list改變後,partition之後的結果也會随着改變
//step.4 List.partition()在真實項目中的運用
List<Integer> numList4 = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9);
List<List<Integer>> partList4 = Lists.partition(numList4, 100);
if (!CollectionUtils.isEmpty(partList4)) {
for (List<Integer> list : partList4) {
//将切割的集合按照固定數量查詢資料庫
//xxxx.findById(list)
//select * from user u where u.id in (1,2,3 ....) 這裡的id數量不要超過100個
}
}
}
}