集合工具类

说明

集合常用方法工具类


代码

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;
    }
}
相关推荐
HalvmånEver6 小时前
Linux:线程创建与终止上(线程五)
java·linux·jvm
m0_748233177 小时前
PHP版本演进:从7.x到8.x全解析
java·开发语言·php
qq_12498707537 小时前
基于springboot的林业资源管理系统设计与实现(源码+论文+部署+安装)
java·vue.js·spring boot·后端·spring·毕业设计·计算机毕业设计
当战神遇到编程7 小时前
图书管理系统
java·开发语言·单例模式
indexsunny7 小时前
互联网大厂Java求职面试实战:Spring Boot微服务与Kafka消息队列应用解析
java·数据库·spring boot·微服务·面试·kafka·jpa
shuair7 小时前
springboot整合redisson单机模式
java·spring boot·后端
Remember_9937 小时前
Java 单例模式深度解析:设计原理、实现范式与企业级应用场景
java·开发语言·javascript·单例模式·ecmascript
代码or搬砖7 小时前
ReentranLock中AQS讲解
java·开发语言·redis
rainbow68897 小时前
C++智能指针实战:从入门到精通
java·开发语言
HalvmånEver7 小时前
Linux:进程 vs 线程:资源共享与独占全解析(线程四)
java·linux·运维