From 8b3f34674288db51d6b7462e53ebe562a0457388 Mon Sep 17 00:00:00 2001 From: VampireAchao Date: Thu, 18 Nov 2021 17:33:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0CollStreamUtil=E5=B9=B6?= =?UTF-8?q?=E8=A1=8C=E6=B5=81=E7=9B=B8=E5=85=B3=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/collection/CollStreamUtil.java | 176 ++++++++++++++++++ 1 file changed, 176 insertions(+) diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/CollStreamUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/CollStreamUtil.java index 53243cb71..9be5a6413 100644 --- a/hutool-core/src/main/java/cn/hutool/core/collection/CollStreamUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/CollStreamUtil.java @@ -33,6 +33,29 @@ public class CollStreamUtil { return collection.stream().collect(Collectors.toMap(key, Function.identity(), (l, r) -> l)); } + + /** + * 将collection转化为类型不变的map
+ * {@code Collection ----> Map} + * + * @param collection 需要转化的集合 + * @param key V类型转化为K类型的lambda方法 + * @param isParallel 是否并行流 + * @param collection中的泛型 + * @param map中的key类型 + * @return 转化后的map + * @see #toIdentityMap toIdentityMap的并行流实现 + */ + public static Map toIdentityMap(Collection collection, Function key, boolean isParallel) { + if (false == isParallel) { + return toIdentityMap(collection, key); + } + if (CollUtil.isEmpty(collection)) { + return Collections.emptyMap(); + } + return collection.parallelStream().collect(Collectors.toMap(key, Function.identity(), (l, r) -> l)); + } + /** * 将Collection转化为map(value类型与collection的泛型不同)
* {@code Collection -----> Map } @@ -52,6 +75,30 @@ public class CollStreamUtil { return collection.stream().collect(Collectors.toMap(key, value, (l, r) -> l)); } + /** + * @param collection 需要转化的集合 + * @param key E类型转化为K类型的lambda方法 + * @param value E类型转化为V类型的lambda方法 + * @param isParallel 是否并行流 + * @param collection中的泛型 + * @param map中的key类型 + * @param map中的value类型 + * @return 转化后的map + * @see #toMap toMap的并行流实现 + * 将Collection转化为map(value类型与collection的泛型不同)
+ * {@code Collection -----> Map } + */ + public static Map toMap(Collection collection, Function key, Function value, boolean isParallel) { + if (false == isParallel) { + return toMap(collection, key, value); + } + if (CollUtil.isEmpty(collection)) { + return Collections.emptyMap(); + } + return collection.parallelStream().collect(Collectors.toMap(key, value, (l, r) -> l)); + } + + /** * 将collection按照规则(比如有相同的班级id)分类成map
* {@code Collection -------> Map> } @@ -71,6 +118,30 @@ public class CollStreamUtil { .collect(Collectors.groupingBy(key, Collectors.toList())); } + /** + * 将collection按照规则(比如有相同的班级id)分类成map
+ * {@code Collection -------> Map> } + * + * @param collection 需要分类的集合 + * @param key 分类的规则 + * @param isParallel 是否并行流 + * @param collection中的泛型 + * @param map中的key类型 + * @return 分类后的map + * @see #groupByKey groupByKey的并行流实现 + */ + public static Map> groupByKey(Collection collection, Function key, boolean isParallel) { + if (false == isParallel) { + return groupByKey(collection, key); + } + if (CollUtil.isEmpty(collection)) { + return Collections.emptyMap(); + } + return collection + .parallelStream() + .collect(Collectors.groupingBy(key, Collectors.toList())); + } + /** * 将collection按照两个规则(比如有相同的年级id,班级id)分类成双层map
* {@code Collection ---> Map>> } @@ -92,6 +163,33 @@ public class CollStreamUtil { .collect(Collectors.groupingBy(key1, Collectors.groupingBy(key2, Collectors.toList()))); } + + /** + * 将collection按照两个规则(比如有相同的年级id,班级id)分类成双层map
+ * {@code Collection ---> Map>> } + * + * @param collection 需要分类的集合 + * @param key1 第一个分类的规则 + * @param key2 第二个分类的规则 + * @param isParallel 是否并行流 + * @param 集合元素类型 + * @param 第一个map中的key类型 + * @param 第二个map中的key类型 + * @return 分类后的map + * @see #groupBy2Key groupBy2Key的并行实现 + */ + public static Map>> groupBy2Key(Collection collection, Function key1, Function key2, boolean isParallel) { + if (false == isParallel) { + return groupBy2Key(collection, key1, key2); + } + if (CollUtil.isEmpty(collection)) { + return Collections.emptyMap(); + } + return collection + .parallelStream() + .collect(Collectors.groupingBy(key1, Collectors.groupingBy(key2, Collectors.toList()))); + } + /** * 将collection按照两个规则(比如有相同的年级id,班级id)分类成双层map
* {@code Collection ---> Map> } @@ -113,6 +211,32 @@ public class CollStreamUtil { .collect(Collectors.groupingBy(key1, Collectors.toMap(key2, Function.identity(), (l, r) -> l))); } + /** + * 将collection按照两个规则(比如有相同的年级id,班级id)分类成双层map
+ * {@code Collection ---> Map> } + * + * @param collection 需要分类的集合 + * @param key1 第一个分类的规则 + * @param key2 第二个分类的规则 + * @param isParallel 是否并行流 + * @param 第一个map中的key类型 + * @param 第二个map中的key类型 + * @param collection中的泛型 + * @return 分类后的map + * @see #group2Map 的并行实现 + */ + public static Map> group2Map(Collection collection, Function key1, Function key2, boolean isParallel) { + if (false == isParallel) { + return group2Map(collection, key1, key2); + } + if (CollUtil.isEmpty(collection) || key1 == null || key2 == null) { + return Collections.emptyMap(); + } + return collection + .parallelStream() + .collect(Collectors.groupingBy(key1, Collectors.toMap(key2, Function.identity(), (l, r) -> l))); + } + /** * 将collection转化为List集合,但是两者的泛型不同
* {@code Collection ------> List } @@ -134,6 +258,32 @@ public class CollStreamUtil { .collect(Collectors.toList()); } + /** + * 将collection转化为List集合,但是两者的泛型不同
+ * {@code Collection ------> List } + * + * @param collection 需要转化的集合 + * @param function collection中的泛型转化为list泛型的lambda表达式 + * @param isParallel 是否并行流 + * @param collection中的泛型 + * @param List中的泛型 + * @return 转化后的list + * @see #toList 的并行实现 + */ + public static List toList(Collection collection, Function function, boolean isParallel) { + if (false == isParallel) { + return toList(collection, function); + } + if (CollUtil.isEmpty(collection)) { + return Collections.emptyList(); + } + return collection + .parallelStream() + .map(function) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } + /** * 将collection转化为Set集合,但是两者的泛型不同
* {@code Collection ------> Set } @@ -155,6 +305,32 @@ public class CollStreamUtil { .collect(Collectors.toSet()); } + /** + * 将collection转化为Set集合,但是两者的泛型不同
+ * {@code Collection ------> Set } + * + * @param collection 需要转化的集合 + * @param function collection中的泛型转化为set泛型的lambda表达式 + * @param isParallel 是否并行流 + * @param collection中的泛型 + * @param Set中的泛型 + * @return 转化后的Set + * @see #toSet 的并行实现 + */ + public static Set toSet(Collection collection, Function function, boolean isParallel) { + if (false == isParallel) { + return toSet(collection, function); + } + if (CollUtil.isEmpty(collection) || function == null) { + return Collections.emptySet(); + } + return collection + .parallelStream() + .map(function) + .filter(Objects::nonNull) + .collect(Collectors.toSet()); + } + /** * 合并两个相同key类型的map