Skip to content

Instantly share code, notes, and snippets.

@codethereforam
Created January 14, 2022 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codethereforam/1f6a82b6fea40abcc8f041b18b7e9ea1 to your computer and use it in GitHub Desktop.
Save codethereforam/1f6a82b6fea40abcc8f041b18b7e9ea1 to your computer and use it in GitHub Desktop.
使list至少有一个元素
/**
* 如果一个集合为空则返回填充一个对象的新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