diff --git a/CHANGELOG.md b/CHANGELOG.md index 688101d0d..cd6f79271 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ * 【bom 】 支持scope=import方式引入(issue#1561@Github) * 【core 】 新增Hash接口,HashXXX继承此接口 * 【core 】 ZipUtil增加append方法(pr#441@Gitee) +* 【core 】 CollUtil增加重载(issue#I4E9FS@Gitee) ### 🐞Bug修复 * 【core 】 修复CollUtil.isEqualList两个null返回错误问题(issue#1885@Github) diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java index 7ab4a9c23..94e992aa1 100644 --- a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java @@ -53,6 +53,7 @@ import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.LinkedBlockingDeque; import java.util.function.Function; import java.util.function.Predicate; +import java.util.function.Supplier; /** * 集合相关工具类 @@ -1050,8 +1051,8 @@ public class CollUtil { * @param end 结束位置(不包含) * @param step 步进 * @return 截取后的数组,当开始位置超过最大时,返回空的List - * @since 4.0.6 * @see ListUtil#sub(List, int, int, int) + * @since 4.0.6 */ public static List sub(List list, int start, int end, int step) { return ListUtil.sub(list, start, end, step); @@ -1073,11 +1074,11 @@ public class CollUtil { /** * 截取集合的部分 * - * @param 集合元素类型 - * @param collection 被截取的数组 - * @param start 开始位置(包含) - * @param end 结束位置(不包含) - * @param step 步进 + * @param 集合元素类型 + * @param collection 被截取的数组 + * @param start 开始位置(包含) + * @param end 结束位置(不包含) + * @param step 步进 * @return 截取后的数组,当开始位置超过最大时,返回空集合 * @since 4.0.6 */ @@ -1086,7 +1087,7 @@ public class CollUtil { return ListUtil.empty(); } - final List list = collection instanceof List ? (List)collection : ListUtil.toList(collection); + final List list = collection instanceof List ? (List) collection : ListUtil.toList(collection); return sub(list, start, end, step); } @@ -1575,6 +1576,20 @@ public class CollUtil { return isEmpty(collection) ? defaultCollection : collection; } + /** + * 如果给定集合为空,返回默认集合 + * + * @param 集合类型 + * @param 集合元素类型 + * @param collection 集合 + * @param supplier 默认值懒加载函数 + * @return 非空(empty)的原集合或默认集合 + * @since 5.7.15 + */ + public static , E> T defaultIfEmpty(T collection, Supplier supplier) { + return isEmpty(collection) ? supplier.get() : collection; + } + /** * Iterable是否为空 * @@ -2948,8 +2963,8 @@ public class CollUtil { * @since 5.6.0 */ public static boolean isEqualList(final Collection list1, final Collection list2) { - if (list1 == list2){ - return true; + if (list1 == list2) { + return true; } if (list1 == null || list2 == null || list1.size() != list2.size()) { return false;