美文网首页
集合数据批量处理工具类

集合数据批量处理工具类

作者: rainy618 | 来源:发表于2018-07-26 22:21 被阅读0次

package com.woqutz.didi.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.commons.lang.ArrayUtils;

public class CollectionUtils {

/**
 * 分批处理数组的工具类
 * 
 * @param collections
 *            需要处理的数组集合
 * @param callBack
 *            处理数组的回调函数
 * @param size
 *            每批处理的个数
 */
@SuppressWarnings("unchecked")
public static <E extends Object> void splitCollectionHandle(List<E> collections,
        ListCollectionCallback<E> callBack, int size) {
    if (collections == null || collections.isEmpty())
        return;
    int collectionSize = collections.size();
    if (collectionSize < size) {
        callBack.call(collections);
        return;
    }
    int forCount = collectionSize / size;
    int mode = collectionSize % size;
    Object[] objectArr = collections.toArray();
    for (int i = 0; i < forCount; i++) {
        Object[] tempArr = ArrayUtils.subarray(objectArr, i * size, (i + 1) * size);
        List<E> objectList = (List<E>) Arrays.asList(tempArr);
        callBack.call(objectList);
    }
    if (mode > 0) {
        Object[] tempArr = ArrayUtils.subarray(objectArr, size * forCount, size * forCount + mode);
        List<E> objectList = (List<E>) Arrays.asList(tempArr);
        callBack.call(objectList);
    }
}
/**
 * 分组调用callBack里的业务,异步调用,全部组调用完后同步
 * 
 * @param collections
 *            需要处理的数组集合
 * @param callBack
 *            处理数组的回调函数
 * @param size
 *            每批处理的个数
 */
@SuppressWarnings("unchecked")
public static <E extends Object> void splitCollectionAsyncCall(List<E> collections,
        ListCollectionCallback<E> callBack, int size) throws InterruptedException, ExecutionException,Exception{
    if (collections == null || collections.isEmpty())
        return;
    int collectionSize = collections.size();
    if (collectionSize < size) {
        callBack.call(collections);
        return;
    }
    
    class AsyncCall implements Runnable{
        ListCollectionCallback<E> callBack=null;
        List<E> objectList = null;
        
        public AsyncCall(ListCollectionCallback<E> callBack,List<E> objectList){
            this.callBack =callBack;
            this.objectList = objectList;
        }
            
        public void run(){
             callBack.call(objectList);
        }
    }
    
    int forCount = collectionSize / size; 
    int mode = collectionSize % size;
    final ExecutorService exePool = Executors.newFixedThreadPool(forCount+(mode>0?1:0));//每个组一个线程
    final List<Future<?>> dataList = new ArrayList<Future<?>>();
    
    
    Object[] objectArr = collections.toArray();
    for (int i = 0; i < forCount; i++) {
        Object[] tempArr = ArrayUtils.subarray(objectArr, i * size, (i + 1) * size);
        List<E> objectList = (List<E>) Arrays.asList(tempArr);
        
        Future<?> future = (Future<?>) exePool.submit(new AsyncCall(callBack,objectList)) ;
       
        
        dataList.add(future);
    }
    if (mode > 0) {
        Object[] tempArr = ArrayUtils.subarray(objectArr, size * forCount, size * forCount + mode);
        List<E> objectList = (List<E>) Arrays.asList(tempArr);
        Future<?> future = (Future<?>) exePool.submit(new AsyncCall(callBack,objectList)) ;
        dataList.add(future);
    }
    List<Exception> exceptionlist = new ArrayList<Exception>();
    for (Future<?> future : dataList) {//同步
        try {
            future.get();
        } catch (InterruptedException e) {
            exceptionlist.add(e);
        } catch (ExecutionException e) {
            exceptionlist.add(e);
        } catch (Exception e) {
            exceptionlist.add(e);
        }
    }
    if(!exceptionlist.isEmpty()){
        exePool.shutdown();
        throw exceptionlist.get(0);
    }
    exePool.shutdown();
    
}
public static void main(String[] args){
    
    return;
}
public static <E extends Object> void splitCollectionHandle(Collection<E> collections,
        CollectionCallback<E> callBack, int size) {
    if (collections == null || collections.isEmpty())
        return;
    int collectionSize = collections.size();
    if (collectionSize < size) {
        callBack.call(collections);
        return;
    }
    int forCount = collectionSize / size;
    int mode = collectionSize % size;
    Object[] objectArr = collections.toArray();
    for (int i = 0; i < forCount; i++) {
        Object[] tempArr = ArrayUtils.subarray(objectArr, i * size, (i + 1) * size);
        List<E> objectList = (List<E>) Arrays.asList(tempArr);
        callBack.call(objectList);
    }
    if (mode > 0) {
        Object[] tempArr = ArrayUtils.subarray(objectArr, size * forCount, size * forCount + mode);
        List<E> objectList = (List<E>) Arrays.asList(tempArr);
        callBack.call(objectList);
    }
}

public static Map<String, Object> changeDbStyleKeyToJavaStyleKey(Map<String, Object> sourceMap) {
    if (null == sourceMap) {
        return sourceMap;
    }
    Map<String, Object> targetMap = new HashMap<String, Object>();
    for (Entry<String, Object> entry : sourceMap.entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();
        targetMap.put(StringUtil.changeDbcloumStyleToJavaStyle(key), value);
    }
    return targetMap;
}
/**
 * 把LIST中类型为简单对象类型INTERGER,LONG ...里的值转为逗号分隔的串 1,2,3
 * 满足IN查询需要
 * @param list
 * @return NULL或SIZE<1时返回 ''
 * @create_time 2012-9-22 下午04:43:39
 */
public static String listToString(List<?> list){
    if(list==null || list.size()<1){
        return "";
    }
    StringBuffer s = new StringBuffer(512);
    for(Object o :list){
        s.append(o.toString().trim()+",");
    }
    if(s.length()>0) s.deleteCharAt(s.length()-1);
    return s.toString();
}

public static String collectionToString(Collection<?> list){
    if(list==null || list.size()<1){
        return "";
    }
    StringBuffer s = new StringBuffer(512);
    for(Object o :list){
        s.append(o.toString().trim()+",");
    }
    if(s.length()>0) s.deleteCharAt(s.length()-1);
    return s.toString();
}

/***
 * 根据Map-value排序
 * @param oriMap
 * @return
 */
public static Map<Integer,Integer> sortMapByValue(Map<Integer,Integer> oriMap) {  
    Map<Integer,Integer> sortedMap = new LinkedHashMap<Integer,Integer>();  
    if (oriMap != null && !oriMap.isEmpty()) {  
        List<Map.Entry<Integer,Integer>> entryList = new ArrayList<Map.Entry<Integer,Integer>>(oriMap.entrySet());  
        Collections.sort(entryList,  
                new Comparator<Map.Entry<Integer,Integer>>() {  
                    public int compare(Entry<Integer,Integer> entry1,  
                            Entry<Integer,Integer> entry2) {  

                        return entry1.getValue() - entry2.getValue();  
                    }  
                });  
        Iterator<Map.Entry<Integer,Integer>> iter = entryList.iterator();  
        Map.Entry<Integer,Integer> tmpEntry = null;  
        while (iter.hasNext()) {  
            tmpEntry = iter.next();  
            sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());  
        }  
    }  
    return sortedMap;  

}
}

相关文章

网友评论

      本文标题:集合数据批量处理工具类

      本文链接:https://www.haomeiwen.com/subject/ojersftx.html