From c8e1d9ac5973985860ae78396ecc4073abce043c Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 29 Dec 2024 22:07:05 +0800 Subject: [PATCH] =?UTF-8?q?CollectionTools=20=E6=96=B0=E5=A2=9E=20nullToEm?= =?UTF-8?q?ptyList=E3=80=81nullToEmptySet=E3=80=81nullToEmptyMap=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `CollectionTools` 提供 `nullToEmptyList`、`nullToEmptySet`、`nullToEmptyMap`,分别在提供的集合为 `null` 时,返回 `Collections.emptyList()`、`Collections.emptySet()`、`Collections.emptyMap()`。 close #7 --- .../commons/collection/CollectionTools.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/collection/CollectionTools.java b/src/main/java/xyz/zhouxy/plusone/commons/collection/CollectionTools.java index d15aaee..f5e671b 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/collection/CollectionTools.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/collection/CollectionTools.java @@ -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 List nullToEmptyList(@Nullable List list) { + return list == null ? Collections.emptyList() : list; + } + + @Nonnull + public static Set nullToEmptySet(@Nullable Set set) { + return set == null ? Collections.emptySet() : set; + } + + @Nonnull + public static Map nullToEmptyMap(@Nullable Map map) { + return map == null ? Collections.emptyMap() : map; + } + private CollectionTools() { throw new IllegalStateException("Utility class"); }