update 优化 StreamUtils 方法过滤null值

This commit is contained in:
疯狂的狮子Li 2023-07-10 09:57:14 +08:00
parent 7f73bfc478
commit cb54a9576e

View File

@ -30,6 +30,7 @@ public class StreamUtils {
if (CollUtil.isEmpty(collection)) { if (CollUtil.isEmpty(collection)) {
return CollUtil.newArrayList(); return CollUtil.newArrayList();
} }
// 注意此处不要使用 .toList() 新语法 因为返回的是不可变List 会导致序列化问题
return collection.stream().filter(function).collect(Collectors.toList()); return collection.stream().filter(function).collect(Collectors.toList());
} }
@ -70,7 +71,8 @@ public class StreamUtils {
if (CollUtil.isEmpty(collection)) { if (CollUtil.isEmpty(collection)) {
return CollUtil.newArrayList(); 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)) { if (CollUtil.isEmpty(collection)) {
return MapUtil.newHashMap(); 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)) { if (CollUtil.isEmpty(collection)) {
return MapUtil.newHashMap(); 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 MapUtil.newHashMap();
} }
return collection return collection
.stream() .stream().filter(Objects::nonNull)
.collect(Collectors.groupingBy(key, LinkedHashMap::new, Collectors.toList())); .collect(Collectors.groupingBy(key, LinkedHashMap::new, Collectors.toList()));
} }
@ -145,7 +147,7 @@ public class StreamUtils {
return MapUtil.newHashMap(); return MapUtil.newHashMap();
} }
return collection return collection
.stream() .stream().filter(Objects::nonNull)
.collect(Collectors.groupingBy(key1, LinkedHashMap::new, Collectors.groupingBy(key2, LinkedHashMap::new, Collectors.toList()))); .collect(Collectors.groupingBy(key1, LinkedHashMap::new, Collectors.groupingBy(key2, LinkedHashMap::new, Collectors.toList())));
} }
@ -166,7 +168,7 @@ public class StreamUtils {
return MapUtil.newHashMap(); return MapUtil.newHashMap();
} }
return collection return collection
.stream() .stream().filter(Objects::nonNull)
.collect(Collectors.groupingBy(key1, LinkedHashMap::new, Collectors.toMap(key2, Function.identity(), (l, r) -> l))); .collect(Collectors.groupingBy(key1, LinkedHashMap::new, Collectors.toMap(key2, Function.identity(), (l, r) -> l)));
} }
@ -188,6 +190,7 @@ public class StreamUtils {
.stream() .stream()
.map(function) .map(function)
.filter(Objects::nonNull) .filter(Objects::nonNull)
// 注意此处不要使用 .toList() 新语法 因为返回的是不可变List 会导致序列化问题
.collect(Collectors.toList()); .collect(Collectors.toList());
} }