add method

This commit is contained in:
Looly 2021-10-21 03:13:52 +08:00
parent 25118070a3
commit 64729aad63
2 changed files with 25 additions and 9 deletions

View File

@ -15,6 +15,7 @@
* 【bom 】 支持scope=import方式引入issue#1561@Github * 【bom 】 支持scope=import方式引入issue#1561@Github
* 【core 】 新增Hash接口HashXXX继承此接口 * 【core 】 新增Hash接口HashXXX继承此接口
* 【core 】 ZipUtil增加append方法pr#441@Gitee * 【core 】 ZipUtil增加append方法pr#441@Gitee
* 【core 】 CollUtil增加重载issue#I4E9FS@Gitee
### 🐞Bug修复 ### 🐞Bug修复
* 【core 】 修复CollUtil.isEqualList两个null返回错误问题issue#1885@Github * 【core 】 修复CollUtil.isEqualList两个null返回错误问题issue#1885@Github

View File

@ -53,6 +53,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.LinkedBlockingDeque;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.Supplier;
/** /**
* 集合相关工具类 * 集合相关工具类
@ -1050,8 +1051,8 @@ public class CollUtil {
* @param end 结束位置不包含 * @param end 结束位置不包含
* @param step 步进 * @param step 步进
* @return 截取后的数组当开始位置超过最大时返回空的List * @return 截取后的数组当开始位置超过最大时返回空的List
* @since 4.0.6
* @see ListUtil#sub(List, int, int, int) * @see ListUtil#sub(List, int, int, int)
* @since 4.0.6
*/ */
public static <T> List<T> sub(List<T> list, int start, int end, int step) { public static <T> List<T> sub(List<T> list, int start, int end, int step) {
return ListUtil.sub(list, start, end, step); return ListUtil.sub(list, start, end, step);
@ -1575,6 +1576,20 @@ public class CollUtil {
return isEmpty(collection) ? defaultCollection : collection; return isEmpty(collection) ? defaultCollection : collection;
} }
/**
* 如果给定集合为空返回默认集合
*
* @param <T> 集合类型
* @param <E> 集合元素类型
* @param collection 集合
* @param supplier 默认值懒加载函数
* @return 非空empty的原集合或默认集合
* @since 5.7.15
*/
public static <T extends Collection<E>, E> T defaultIfEmpty(T collection, Supplier<? extends T> supplier) {
return isEmpty(collection) ? supplier.get() : collection;
}
/** /**
* Iterable是否为空 * Iterable是否为空
* *