Java-大集合拆分为指定大小的小集合
因为Oracle数据的in 最大允许1000 ,超过就会报错, 所以需要将集合拆分为多个集合进行处理.
/** * 拆分集合 * @param <T> * @param resList 要拆分的集合 * @param count 每个集合的元素个数 * @return 返回拆分后的各个集合 */ public static <T> List<List<T>> split(List<T> resList,int count){ if(resList==null ||count<1) return null ; List<List<T>> ret=new ArrayList<List<T>>(); int size=resList.size(); if(size<=count){ //数据量不足count指定的大小 ret.add(resList); }else{ int pre=size/count; int last=size%count; //前面pre个集合,每个大小都是count个元素 for(int i=0;i<pre;i++){ List<T> itemList=new ArrayList<T>(); for(int j=0;j<count;j++){ itemList.add(resList.get(i*count+j)); } ret.add(itemList); } //last的进行处理 if(last>0){ List<T> itemList=new ArrayList<T>(); for(int i=0;i<last;i++){ itemList.add(resList.get(pre*count+i)); } ret.add(itemList); } } return ret; }
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub List<String> resList=Arrays.asList("0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87","88","89","90","91","92","93","94","95","96","97","98","99"); List<List<String>> ret=split(resList,10); for(int i=0;i<ret.size();i++){ System.out.println(ret.get(i)); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。