集合工具类

说明

集合常用方法工具类


代码

java 复制代码
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

public class CollectionUtil {
    /**
     * 拆分list
     *
     * @param list list
     * @param size 拆分后的子list长度
     * @param <T>  数据类型
     * @return 拆分后的list集合
     */
    public static <T> List<List<T>> splitList(List<T> list, int size) {
        List<List<T>> ret = new ArrayList<>();
        if (size <= 0) {
            ret.add(list);
            return ret;
        }
        if (isEmpty(list)) {
            return ret;
        }
        int index = 0;
        do {
            ret.add(list.subList(index * size, Math.min(list.size(), (index + 1) * size)));
            index++;
        } while (index * size < list.size());
        return ret;
    }

    /**
     * 集合是否为空
     *
     * @param collection 集合数据
     * @param <T>        数据类型
     * @return true-空/false-不为空
     */
    public static <T> boolean isEmpty(Collection<T> collection) {
        return collection == null || collection.isEmpty();
    }

    /**
     * 获取在leftCollection而不在rightCollection的元素集合
     *
     * @param leftCollection  leftCollection
     * @param rightCollection rightCollection
     * @param <T>             数据类型
     * @return 在leftCollection而不在rightCollection的元素集合
     */
    public static <T> Collection<T> complementary(Collection<T> leftCollection, Collection<T> rightCollection) {
        if (leftCollection == null) {
            leftCollection = Collections.emptyList();
        }
        if (rightCollection == null) {
            rightCollection = Collections.emptyList();
        }
        Collection<T> collection = new ArrayList<>(leftCollection);
        collection.removeAll(rightCollection);
        return collection;
    }

    /**
     * 获取leftCollection和rightCollection的交集
     *
     * @param leftCollection  leftCollection
     * @param rightCollection rightCollection
     * @param <T>             数据类型
     * @return 同时存在于leftCollection和rightCollection的元素
     */
    public static <T> Collection<T> intersection(Collection<T> leftCollection, Collection<T> rightCollection) {
        if (isEmpty(leftCollection) || isEmpty(rightCollection)) {
            return new ArrayList<>();
        }
        Collection<T> collection = new ArrayList<>(leftCollection);
        collection.retainAll(rightCollection);
        return collection;
    }

    /**
     * 合并leftCollection和rightCollection两个集合
     *
     * @param leftCollection  leftCollection
     * @param rightCollection rightCollection
     * @param <T>             数据类型
     * @return leftCollection和rightCollection的并集
     */
    public static <T> Collection<T> union(Collection<T> leftCollection, Collection<T> rightCollection) {
        if (leftCollection == null) {
            leftCollection = Collections.emptyList();
        }
        if (rightCollection == null) {
            rightCollection = Collections.emptyList();
        }
        Collection<T> collection = complementary(leftCollection, rightCollection);
        collection.addAll(rightCollection);
        return collection;
    }

    /**
     * 移除null元素
     * 集合有方法collection.removeIf()可以调用
     *
     * @param collection 集合
     * @param <T>        数据类型
     */
    public static <T> void removeNull(Collection<T> collection) {
        if (collection == null) {
            return;
        }
        Iterator<T> iterator = collection.iterator();
        while (iterator.hasNext()) {
            T next = iterator.next();
            if (next == null) {
                iterator.remove();
            }
        }
    }

    /**
     * 比较两个集合的元素是否相同
     *
     * @param leftCollection  leftCollection
     * @param rightCollection rightCollection
     * @param <T>             数据类型
     * @return true-相同/false-不同
     */
    public static <T> boolean equals(Collection<T> leftCollection, Collection<T> rightCollection) {
        if (leftCollection == rightCollection) {
            return true;
        }
        if (leftCollection == null || rightCollection == null) {
            return false;
        }
        if (leftCollection.isEmpty() && rightCollection.isEmpty()) {
            return true;
        }
        if (leftCollection.size() != rightCollection.size()) {
            return false;
        }
        List<T> leftSorted = leftCollection.stream()
                .sorted((o1, o2) -> o1 == null ? 1 : o2 == null ? -1 : o1.hashCode() - o2.hashCode())
                .toList();
        List<T> rightSorted = rightCollection.stream()
                .sorted((o1, o2) -> o1 == null ? 1 : o2 == null ? -1 : o1.hashCode() - o2.hashCode())
                .toList();
        for (int index = 0; index < leftSorted.size(); index++) {
            T left = leftSorted.get(index);
            T right = rightSorted.get(index);
            if (!Objects.equals(left, right)) {
                return false;
            }
        }
        return true;
    }
}
相关推荐
rannn_1115 小时前
【力扣hot100】链表专题|160、206、234、141、142
java·算法·leetcode·链表·面试·开发
IKUN家族7 小时前
Spring IoC&DI
java·spring·rpc
山荷枝8 小时前
03-框架--Spring
java·后端·spring
ChaHae-In9 小时前
SpringBoot统一功能处理
java·spring boot·后端
coder!mq9 小时前
说几个常见的语法糖?
java·开发语言·算法
gugucoding9 小时前
38. 【Java】Stream API(下):高级操作与性能
java·开发语言
心平气和量大福大9 小时前
android-实例-蒲公英-更新与安装-5-签名与生成APK
android·java·开发语言
9523610 小时前
Spring-cloud配置中心
java·spring·spring cloud·微服务
ydd10010010 小时前
寻找两个正序数组的中位数 Java 题解,二分分割详解
java·开发语言
真上帝的左手11 小时前
10. 软件设计&架构-Spring Security 7 整合CAS SSO 单点登录
java·spring·架构·sso