封装一个方法,用一个Map来实现,这里是根据bean类的seq字段进行拆分的,分成好几个list
private LinkedHashMap<String,List<HandleInfo>> groupListBySeq(List<HandleInfo> list) {
LinkedHashMap<String,List<HandleInfo>> map = new LinkedHashMap<String,List<HandleInfo>>();
for (HandleInfo bean : list) {
if(map.containsKey(bean.getSeq())){
List<HandleInfo> subList = map.get(bean.getSeq());
subList.add(bean);
}else{
List<HandleInfo> subList = new ArrayList<HandleInfo>();
subList.add(bean);
map.put(bean.getSeq(), subList);
}
}
return map;
}
然后可以对获取到的Map进行遍历:
LinkedHashMap<String,List<HandleInfo>> map = groupListBySeq(needUpdateHandleInfoList);
//遍历集合
for(Map.Entry<String, List<HandleInfo>> entry : map.entrySet()){
List<HandleInfo> list=(List<HandleInfo>)entry.getValue();
HandleInfo bean0 = new HandleInfo();
if(null != list || !list.isEmpty()){
bean0 = list.get(0);
}
for(HandleInfo handleInfoModel : list){
...
}
}
上面是按数据库字段进行分组的方法,然后介绍一下平均分成多少数据量的集合
java代码只要获取参数进行集合拆分就可以:
举个例子,下面是一种方法,将list按照没1000个为一个集合分组
List<String> values = new ArrayList<String>();
String[] configSeqArray = StringUtils.split(configSeq,',');
for (String str : configSeqArray) {
values.add(str);
}
List<Collection<String>> configSeqs = CollectionUtil.splitCollection(values, 1000);
复制公司同事写的集合拆分的方法
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class CollectionUtils {
public static List<Collection<String>> splitCollection(Collection<String>values , int size) {
List<Collection<String>> result = new ArrayList<Collection<String>>();
if(values.size() <= size ){
result.add(values);
}else{
int count =0;
Collection<String> subCollection= null;
for(String s:c){
if(subCollection == null){
subColletion = new ArrayList<String>();
result.add(subColletion);
}
subCollection.add(s);
count++;
if(count == size){
count =0;
subCollectiion = null;
}
}
}
}
}
这种方法可以应用与解决Oracle select in超过1000个的报错,具体参考:https://smilenicky.blog.csdn.net/article/details/87922878
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/17035.html