欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JAVA 集合操作工具类

程序员文章站 2024-01-14 10:34:40
...

JAVA 集合操作工具类

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;

import java.lang.reflect.Method;
import java.util.*;

/**
 * @Author Mc 
 * @Date 2021/1/26 下午6:17
 * @Version 1.0
 */
@Slf4j
public class SegmentationCollectionUtils extends CollectionUtils {

    /**
     * 讲一个List的集合分割成多个用于批量处理
     * @param source 总量集合
     * @param n 分成几份
     * @param <T> 泛型
     * @return 分割后的子集合
     */
    public static <T> List<List<T>> averageAssignList(List<T> source, int n) {
        List<List<T>> result = new ArrayList<List<T>>();
        int remainder = source.size() % n;  //(先计算出余数)
        int number = source.size() / n;  //然后是商
        int offset = 0;//偏移量
        for (int i = 0; i < n; i++) {
            List<T> value = null;
            if (remainder > 0) {
                value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
                remainder--;
                offset++;
            } else {
                value = source.subList(i * number + offset, (i + 1) * number + offset);
            }
            result.add(value);
        }
        return result;
    }



    private static final int DEFAULT_SIZE = 100;

    /**
     *
     * <p>拆分List为固定大小的多个集合</p>
     * <p>推荐使用</p>
     * <p>返回集合的size越小,此方法性能越高</p>
     * @param baseList
     * @param size
     * @return ArrayList
     */
    @SuppressWarnings("unchecked")
    public static <T> List<List<T>> fastSplitList(List<T> baseList, int size) {
        if (baseList == null || baseList.size() == 0) {
            return null;
        }
        if (size <= 0) {
            size = DEFAULT_SIZE;
        }
        int arrSize = baseList.size() % size == 0 ? baseList.size() / size : baseList.size() / size + 1;
        List<List<T>> resultList = new ArrayList<List<T>>();
        for (int i = 0; i < arrSize; i++) {
            if (arrSize - 1 == i) {
                resultList.add((List<T>) new ArrayList<Object>( baseList.subList(i * size, baseList.size())));
            } else {
                resultList.add((List<T>) new ArrayList<Object>( baseList.subList(i * size, size * (i + 1))));
            }
        }
        return resultList;
    }

    /**
     *
     * <p>拆分List为固定大小的多个集合</p>
     * <p>返回集合的size越大,此方法性能越高</p>
     * @param baseList
     * @param size
     * @return ArrayList
     */
    public static <T> List<List<T>> splitList(List<T> baseList, int size) {
        if (baseList == null || baseList.size() == 0) {
            return null;
        }
        if (size <= 0) {
            size = DEFAULT_SIZE;
        }
        List<List<T>> resultList = new ArrayList<List<T>>();
        for (int i = 0; i < baseList.size(); ++i) {
            if (i % size == 0) {
                List<T> result = new ArrayList<T>();
                resultList.add(result);
            }
            resultList.get(i / size).add(baseList.get(i));
        }
        return resultList;
    }

    /**
     *
     * <p>集合转Set</p>
     * @param coll 源集合
     * @param keyType 属性类型
     * @param keyMethodName 属性get方法
     * @return LinkedHashSet
     */
    public static <K, V> Set<K> asSet(final java.util.Collection<V> coll,final Class<K> keyType
            ,final String keyMethodName) {
        if (CollectionUtils.isEmpty(coll)) {
            return new HashSet<K>(0);
        }
        final Set<K> set = new LinkedHashSet<K>(coll.size());
        try {
            for (final V value : coll) {
                Object object;
                Method method = value.getClass().getMethod(keyMethodName);
                object = method.invoke(value);
                @SuppressWarnings("unchecked")
                final K key = (K) object;
                set.add(key);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new CollectionUtilsException("Collection conversion Set exceptions");
        }
        return set;
    }

    /**
     *
     * <p>集合转Map</p>
     * <p>比如:List<EmployeeEntity>,讲EmployeeEntity的name属性作为key,转换成Map</p>
     * @param coll 源集合
     * @param keyType 属性类型
     * @param valueType 源数据类型(实体类型)
     * @param keyMethodName 属性get方法
     * @return LinkedHashMap
     */
    public static <K, V> Map<K, V> asMap(final java.util.Collection<V> coll,final Class<K> keyType
            ,final Class<V> valueType,final String keyMethodName) {
        if (CollectionUtils.isEmpty(coll)) {
            return new LinkedHashMap<K, V>(0);
        }
        final Map<K, V> map = new LinkedHashMap<K, V>(coll.size());
        try {
            Method method = valueType.getMethod(keyMethodName);
            for (final V value : coll) {
                Object object;
                object = method.invoke(value);
                @SuppressWarnings("unchecked")
                final K key = (K) object;
                map.put(key, value);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new CollectionUtilsException("Collection conversion Map exceptions");
        }
        return map;
    }

    /**
     * <p>集合转List</p>
     * @param coll
     * @return ArrayList
     */
    public static <V> List<V> asList(final java.util.Collection<V> coll) {
        if (CollectionUtils.isEmpty(coll)) {
            return new ArrayList<V>(0);
        }
        final List<V> list = new ArrayList<V>();
        for (final V value : coll) {
            if (value != null) {
                list.add(value);
            }
        }
        return list;
    }

    /**
     * <p>集合<String>toString</p>
     * @param collection 泛型必须为String类型
     * @param split 比如连接符","
     * @return
     */
    public static String collToString(Collection<String> collection, String split) {
        StringBuilder sb = new StringBuilder();
        if (collection != null) {
            int i = 0, size = collection.size();
            for (Iterator<String> iterator = collection.iterator(); iterator.hasNext();) {
                String str = iterator.next();
                sb.append(str);
                if (++i < size) {
                    sb.append(split);
                }
            }
        }
        return sb.toString();
    }

    static class CollectionUtilsException extends RuntimeException{

        private static final long serialVersionUID = 1L;

        public CollectionUtilsException(String s) {
            super(s);
        }

        public CollectionUtilsException(String s, Throwable e) {
            super(s, e);
        }

        public CollectionUtilsException(Throwable e) {
            super(e);
        }

    }

}