CollectionTools 新增 nullToEmptyList、nullToEmptySet、nullToEmptyMap 方法

Reviewed-on: #1
1.x.x
ZhouXY108 2024-12-29 22:09:31 +08:00
commit f7f7bed848
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");
}