集合工具类

说明

集合常用方法工具类


代码

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;
    }
}
相关推荐
算法与双吉汉堡1 分钟前
【短链接项目笔记】Day3 用户模块剩余部分
java·redis·后端
Chengbei111 分钟前
fastjson 原生反序列化配合动态代理绕过限制
java·安全·网络安全·系统安全·安全架构
lhrimperial2 分钟前
MySQL底层原理
java·后端·mysql
qq_377112374 分钟前
JAVA的平凡之路——此峰乃是最高峰JVM-GC垃圾回收器(1)-06
java·开发语言·jvm
学编程就要猛7 分钟前
算法:2.复写零
java·数据结构·算法
韩立学长10 分钟前
【开题答辩实录分享】以《植物园信息管理系统》为例进行选题答辩实录分享
java·数据库·spring
嘻哈baby11 分钟前
记一次线上OOM排查,JVM调优全过程
java
a程序小傲12 分钟前
京东Java面试被问:垃圾收集算法(标记-清除、复制、标记-整理)的比较
java·算法·面试
austin流川枫13 分钟前
深度解析六大Java微服务框架
java·后端·微服务
毕设源码-赖学姐21 分钟前
【开题答辩全过程】以 高校贫困生资助管理系统为例,包含答辩的问题和答案
java·eclipse