forked from plusone/plusone-commons
CollectionTools 新增 nullToEmptyList、nullToEmptySet、nullToEmptyMap 方法
`CollectionTools` 提供 `nullToEmptyList`、`nullToEmptySet`、`nullToEmptyMap`,分别在提供的集合为 `null` 时,返回 `Collections.emptyList()`、`Collections.emptySet()`、`Collections.emptyMap()`。 close #7pull/1/head
parent
6bc32ce379
commit
c8e1d9ac59
|
@ -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");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue