CollectionTools 新增 nullToEmptyList、nullToEmptySet、nullToEmptyMap 方法

`CollectionTools` 提供 `nullToEmptyList`、`nullToEmptySet`、`nullToEmptyMap`,分别在提供的集合为 `null` 时,返回 `Collections.emptyList()`、`Collections.emptySet()`、`Collections.emptyMap()`。

close #7
pull/1/head
ZhouXY108 2024-12-29 22:07:05 +08:00
parent 6bc32ce379
commit c8e1d9ac59
1 changed files with 19 additions and 0 deletions

View File

@ -17,8 +17,12 @@
package xyz.zhouxy.plusone.commons.collection;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
@ -51,6 +55,21 @@ public class CollectionTools {
return map != null && !map.isEmpty();
}
@Nonnull
public static <T> List<T> nullToEmptyList(@Nullable List<T> list) {
return list == null ? Collections.emptyList() : list;
}
@Nonnull
public static <T> Set<T> nullToEmptySet(@Nullable Set<T> set) {
return set == null ? Collections.emptySet() : set;
}
@Nonnull
public static <K, V> Map<K, V> nullToEmptyMap(@Nullable Map<K, V> map) {
return map == null ? Collections.emptyMap() : map;
}
private CollectionTools() {
throw new IllegalStateException("Utility class");
}