/** * 开源代码,仅供学习和交流研究使用,商用请联系三丙 * 微信:mohan_88888 * 抖音:程序员三丙 * 付费课程:https://www.bilibili.com/cheese/play/ss942400790 */ package sanbing.jcpp.infrastructure.util; import java.util.*; import java.util.stream.Collectors; public class CollectionsUtil { /** * 判断集合是否为空(null 或者 size 为 0)。 */ public static boolean isEmpty(Collection collection) { return collection == null || collection.isEmpty(); } /** * 判断集合是否不为空。 */ public static boolean isNotEmpty(Collection collection) { return !isEmpty(collection); } /** * 返回存在于集合 B(新)但不在集合 A(旧)中的元素的新集合。 */ public static Set diffSets(Set a, Set b) { return b.stream().filter(p -> !a.contains(p)).collect(Collectors.toSet()); } /** * 返回存在于列表 B(新)但不在列表 A(旧)中的元素的新列表。 */ public static List diffLists(List a, List b) { return b.stream().filter(p -> !a.contains(p)).collect(Collectors.toList()); } /** * 判断集合中是否包含指定元素。 */ public static boolean contains(Collection collection, T element) { return isNotEmpty(collection) && collection.contains(element); } /** * 统计数组中非空元素的数量。 */ public static int countNonNull(T[] array) { int count = 0; for (T t : array) { if (t != null) count++; } return count; } /** * 创建一个 Map,传入键值对参数。 * 如果参数数量不是偶数,则抛出异常。 */ @SuppressWarnings("unchecked") public static Map mapOf(T... kvs) { if (kvs.length % 2 != 0) { throw new IllegalArgumentException("参数数量无效"); } Map map = new HashMap<>(); for (int i = 0; i < kvs.length; i += 2) { T key = kvs[i]; T value = kvs[i + 1]; map.put(key, value); } return map; } /** * 判断集合是否为空或者包含指定元素。 */ public static boolean emptyOrContains(Collection collection, V element) { return isEmpty(collection) || collection.contains(element); } /** * 合并两个集合并返回一个新的 HashSet。 */ public static HashSet concat(Set set1, Set set2) { HashSet result = new HashSet<>(); result.addAll(set1); result.addAll(set2); return result; } }