集合工具类

说明

集合常用方法工具类


代码

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;
    }
}
相关推荐
子豪-中国机器人3 小时前
《C++ STL 基础入门》教案
java·开发语言
消失的旧时光-19433 小时前
ScheduledExecutorService
android·java·开发语言
勇闯逆流河3 小时前
【C++】用红黑树封装map与set
java·开发语言·数据结构·c++
SpiderPex4 小时前
论MyBatis和JPA权威性
java·mybatis
小猪咪piggy4 小时前
【微服务】(1) Spring Cloud 概述
java·spring cloud·微服务
lkbhua莱克瓦244 小时前
Java基础——面向对象进阶复习知识点8
java·笔记·github·学习方法
m0_736927044 小时前
Spring Boot自动配置与“约定大于配置“机制详解
java·开发语言·后端·spring
GL-Yang5 小时前
2025年-集合类面试题
java·面试
你不是我我5 小时前
【Java 开发日记】我们来说一说 Redisson 的原理
java·开发语言