集合工具类

说明

集合常用方法工具类


代码

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;
    }
}
相关推荐
Hello.Reader12 小时前
Data Sink定义、参数与可落地示例
java·前端·网络
2401_8370885013 小时前
stringRedisTemplate.opsForHash().entries
java·redis
lkbhua莱克瓦2414 小时前
Java基础——集合进阶3
java·开发语言·笔记
蓝-萧15 小时前
使用Docker构建Node.js应用的详细指南
java·后端
多喝开水少熬夜15 小时前
Trie树相关算法题java实现
java·开发语言·算法
lkbhua莱克瓦2415 小时前
Java基础——集合进阶用到的数据结构知识点1
java·数据结构·笔记·github
音符犹如代码16 小时前
Java并发List实战:CopyOnWriteArrayList原理与ArrayList常见面试题
java·开发语言·面试·list
代码or搬砖16 小时前
Docker 部署 Java 项目实践
java·docker·容器
又是忙碌的一天17 小时前
抽象类和接口
java·开发语言
August_._17 小时前
【MySQL】SQL语法详细总结
java·数据库·后端·sql·mysql·oracle