From cb54a9576e9f89ed593333ba9cac8722f502f145 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Mon, 10 Jul 2023 09:57:14 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20StreamUtils=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E8=BF=87=E6=BB=A4null=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/ruoyi/common/utils/StreamUtils.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/StreamUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/StreamUtils.java index a03e5bee4..0f57122eb 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/StreamUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/StreamUtils.java @@ -30,6 +30,7 @@ public class StreamUtils { if (CollUtil.isEmpty(collection)) { return CollUtil.newArrayList(); } + // 注意此处不要使用 .toList() 新语法 因为返回的是不可变List 会导致序列化问题 return collection.stream().filter(function).collect(Collectors.toList()); } @@ -70,7 +71,8 @@ public class StreamUtils { if (CollUtil.isEmpty(collection)) { return CollUtil.newArrayList(); } - return collection.stream().sorted(comparing).collect(Collectors.toList()); + // 注意此处不要使用 .toList() 新语法 因为返回的是不可变List 会导致序列化问题 + return collection.stream().filter(Objects::nonNull).sorted(comparing).collect(Collectors.toList()); } /** @@ -87,7 +89,7 @@ public class StreamUtils { if (CollUtil.isEmpty(collection)) { return MapUtil.newHashMap(); } - return collection.stream().collect(Collectors.toMap(key, Function.identity(), (l, r) -> l)); + return collection.stream().filter(Objects::nonNull).collect(Collectors.toMap(key, Function.identity(), (l, r) -> l)); } /** @@ -106,7 +108,7 @@ public class StreamUtils { if (CollUtil.isEmpty(collection)) { return MapUtil.newHashMap(); } - return collection.stream().collect(Collectors.toMap(key, value, (l, r) -> l)); + return collection.stream().filter(Objects::nonNull).collect(Collectors.toMap(key, value, (l, r) -> l)); } /** @@ -124,7 +126,7 @@ public class StreamUtils { return MapUtil.newHashMap(); } return collection - .stream() + .stream().filter(Objects::nonNull) .collect(Collectors.groupingBy(key, LinkedHashMap::new, Collectors.toList())); } @@ -145,7 +147,7 @@ public class StreamUtils { return MapUtil.newHashMap(); } return collection - .stream() + .stream().filter(Objects::nonNull) .collect(Collectors.groupingBy(key1, LinkedHashMap::new, Collectors.groupingBy(key2, LinkedHashMap::new, Collectors.toList()))); } @@ -166,7 +168,7 @@ public class StreamUtils { return MapUtil.newHashMap(); } return collection - .stream() + .stream().filter(Objects::nonNull) .collect(Collectors.groupingBy(key1, LinkedHashMap::new, Collectors.toMap(key2, Function.identity(), (l, r) -> l))); } @@ -188,6 +190,7 @@ public class StreamUtils { .stream() .map(function) .filter(Objects::nonNull) + // 注意此处不要使用 .toList() 新语法 因为返回的是不可变List 会导致序列化问题 .collect(Collectors.toList()); }