Created
January 14, 2022 09:03
-
-
Save codethereforam/1f6a82b6fea40abcc8f041b18b7e9ea1 to your computer and use it in GitHub Desktop.
使list至少有一个元素
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 如果一个集合为空则返回填充一个对象的新list(不修改原list) | |
* | |
* @param list list | |
* @param constructor constructor | |
* @param <T> type of list's element | |
* @return 至少有一个元素的集合 | |
* @author yanganyu | |
* @date 2022/1/14 | |
*/ | |
private static <T> List<T> fillOneIfEmpty(List<T> list, Supplier<T> constructor) { | |
if (CollectionUtils.isEmpty(list)) { | |
List<T> newList = new ArrayList<>(); | |
newList.add(constructor.get()); | |
return newList; | |
} else { | |
return list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment