package com.tudicloud.framework.common.core.util.core;

import java.util.*;

/**
 * 集合工具类,提供对集合的常用操作方法。
 */
public class CollectionUtils {
    private static Integer INTEGER_ONE = Integer.valueOf(1);



    /**
     * 判断集合是否为空。
     *
     * @param coll 待判断的集合
     * @return 如果集合为null或空则返回true,否则返回false
     */
    public static boolean isEmpty(Collection coll) {
        return (coll == null || coll.isEmpty());
    }


    /**
     * 判断集合是否不为空。
     *
     * @param coll 待判断的集合
     * @return 如果集合不为null且不为空则返回true,否则返回false
     */
    public static boolean isNotEmpty(Collection coll) {
        return !isEmpty(coll);
    }



    /**
     * 安全地检查集合中是否包含指定元素。
     *
     * @param collection 被检查的集合
     * @param value      指定元素
     * @return 如果集合不为空且包含该元素返回true,否则返回false
     */
    public static boolean contains(Collection<?> collection, Object value) {
        return isNotEmpty(collection) && collection.contains(value);
    }


    /**
     * 计算两个集合的并集(考虑重复元素)。
     * 并集中每个元素出现次数等于其在两个集合中最大出现次数。
     *
     * @param a 第一个集合
     * @param b 第二个集合
     * @return 包含所有元素及其最大频率的新集合
     */
    public static Collection union(final Collection a, final Collection b) {
        ArrayList list = new ArrayList();
        Map mapa = getCardinalityMap(a);
        Map mapb = getCardinalityMap(b);
        Set elts = new HashSet(a);
        elts.addAll(b);
        Iterator it = elts.iterator();
        while(it.hasNext()) {
            Object obj = it.next();
            for(int i=0,m=Math.max(getFreq(obj,mapa),getFreq(obj,mapb));i<m;i++) {
                list.add(obj);
            }
        }
        return list;
    }

    /**
     * 获取对象在频率映射中的出现次数。
     *
     * @param obj     查询的对象
     * @param freqMap 对象频率映射表
     * @return 对象出现的次数,默认为0
     */
    private static final int getFreq(final Object obj, final Map freqMap) {
        Integer count = (Integer) freqMap.get(obj);
        if (count != null) {
            return count.intValue();
        }
        return 0;
    }


    /**
     * 构建集合中各元素的出现频次映射表。
     *
     * @param coll 输入集合
     * @return 元素到出现次数的映射关系
     */
    public static Map getCardinalityMap(final Collection coll) {
        Map count = new HashMap();
        for (Iterator it = coll.iterator(); it.hasNext();) {
            Object obj = it.next();
            Integer c = (Integer) (count.get(obj));
            if (c == null) {
                count.put(obj,INTEGER_ONE);
            } else {
                count.put(obj, Integer.valueOf(c.intValue() + 1));
            }
        }
        return count;
    }


    /**
     * 计算两个集合的交集(考虑重复元素)。
     * 交集中每个元素出现次数等于其在两个集合中最小出现次数。
     *
     * @param a 第一个集合
     * @param b 第二个集合
     * @return 包含共有元素及其最小频率的新集合
     */
    public static Collection intersection(final Collection a, final Collection b) {
        ArrayList list = new ArrayList();
        Map mapa = getCardinalityMap(a);
        Map mapb = getCardinalityMap(b);
        Set elts = new HashSet(a);
        elts.addAll(b);
        Iterator it = elts.iterator();
        while(it.hasNext()) {
            Object obj = it.next();
            for(int i=0,m=Math.min(getFreq(obj,mapa),getFreq(obj,mapb));i<m;i++) {
                list.add(obj);
            }
        }
        return list;
    }


    /**
     * 计算两个集合的差集(考虑重复元素)。
     * 差集中每个元素出现次数为其在两集合中出现次数之差的绝对值。
     *
     * @param a 第一个集合
     * @param b 第二个集合
     * @return 包含差异元素及其出现次数差值的新集合
     */
    public static Collection disjunction(final Collection a, final Collection b) {
        ArrayList list = new ArrayList();
        Map mapa = getCardinalityMap(a);
        Map mapb = getCardinalityMap(b);
        Set elts = new HashSet(a);
        elts.addAll(b);
        Iterator it = elts.iterator();
        while(it.hasNext()) {
            Object obj = it.next();
            for(int i=0,m=((Math.max(getFreq(obj,mapa),getFreq(obj,mapb)))-(Math.min(getFreq(obj,mapa),getFreq(obj,mapb))));i<m;i++) {
                list.add(obj);
            }
        }
        return list;
    }


    /**
     * 从第一个集合中移除第二个集合中存在的元素。
     *
     * @param a 原始集合
     * @param b 要被移除元素的集合
     * @return 移除后的新集合副本
     */
    public static Collection subtract(final Collection a, final Collection b) {
        ArrayList list = new ArrayList( a );
        for (Iterator it = b.iterator(); it.hasNext();) {
            list.remove(it.next());
        }
        return list;
    }



    /**
     * 创建一个新的ArrayList实例。
     *
     * @param <E> 泛型类型参数
     * @return 新创建的ArrayList实例
     */
    public static <E> ArrayList<E> newArrayList() {
        return new ArrayList<E>();
    }


    /**
     * 根据给定元素创建一个新的ArrayList实例,并将这些元素添加进去。
     *
     * @param <E>      泛型类型参数
     * @param elements 可变长度的元素数组
     * @return 包含指定元素的新ArrayList实例
     */
    public static <E> ArrayList<E> newArrayList(E... elements) {
        ArrayList<E> list = new ArrayList<E>();
        Collections.addAll(list, elements);
        return list;
    }



    /**
     * 将列表按批次大小分割成多个子列表。
     *
     * @param <E>       列表元素类型
     * @param list      原始列表
     * @param batchSize 批次大小
     * @return 分割后的子列表组成的列表
     */
    public static <E> List<List<E>> split(List<E> list, int batchSize) {
        if (list == null || list.isEmpty() || batchSize <= 0) {
            return Collections.emptyList();
        }

        List<List<E>> result = new ArrayList<>();
        int totalSize = list.size();
        for (int i = 0; i < totalSize; i += batchSize) {
            int end = Math.min(i + batchSize, totalSize);
            result.add(list.subList(i, end));
        }
        return result;
    }


}

最后修改:2025 年 11 月 14 日
如果觉得我的文章对你有用,请随意赞赏