diff --git a/CHANGELOG.md b/CHANGELOG.md index 872cd4e56..66f613e63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ------------------------------------------------------------------------------------------------------------- -# 5.7.17 (2021-11-16) +# 5.7.17 (2021-11-18) ### 🐣新特性 * 【core 】 增加AsyncUtil(pr#457@Gitee) @@ -17,6 +17,7 @@ * 【core 】 改进TextFinder,支持限制结束位置及反向查找模式 * 【core 】 Opt增加部分方法(pr#459@Gitee) * 【core 】 增加DefaultCloneable(pr#459@Gitee) +* 【core 】 CollStreamUtil增加是否并行的重载(pr#467@Gitee) * ### 🐞Bug修复 * 【core 】 修复FileResource构造fileName参数无效问题(issue#1942@Github) 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 9be5a6413..693be7936 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 @@ -2,6 +2,7 @@ package cn.hutool.core.collection; import cn.hutool.core.map.MapUtil; +import cn.hutool.core.stream.StreamUtil; import java.util.*; import java.util.function.BiFunction; @@ -27,10 +28,7 @@ public class CollStreamUtil { * @return 转化后的map */ public static Map toIdentityMap(Collection collection, Function key) { - if (CollUtil.isEmpty(collection)) { - return Collections.emptyMap(); - } - return collection.stream().collect(Collectors.toMap(key, Function.identity(), (l, r) -> l)); + return toIdentityMap(collection, key, false); } @@ -44,16 +42,13 @@ public class CollStreamUtil { * @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)); + return StreamUtil.of(collection, isParallel) + .collect(Collectors.toMap(key, Function.identity(), (l, r) -> l)); } /** @@ -69,10 +64,7 @@ public class CollStreamUtil { * @return 转化后的map */ public static Map toMap(Collection collection, Function key, Function value) { - if (CollUtil.isEmpty(collection)) { - return Collections.emptyMap(); - } - return collection.stream().collect(Collectors.toMap(key, value, (l, r) -> l)); + return toMap(collection, key, value, false); } /** @@ -84,18 +76,12 @@ public class CollStreamUtil { * @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)); + return StreamUtil.of(collection, isParallel).collect(Collectors.toMap(key, value, (l, r) -> l)); } @@ -110,12 +96,7 @@ public class CollStreamUtil { * @return 分类后的map */ public static Map> groupByKey(Collection collection, Function key) { - if (CollUtil.isEmpty(collection)) { - return Collections.emptyMap(); - } - return collection - .stream() - .collect(Collectors.groupingBy(key, Collectors.toList())); + return groupByKey(collection, key, false); } /** @@ -128,18 +109,12 @@ public class CollStreamUtil { * @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())); + return StreamUtil.of(collection, isParallel).collect(Collectors.groupingBy(key, Collectors.toList())); } /** @@ -155,12 +130,7 @@ public class CollStreamUtil { * @return 分类后的map */ public static Map>> groupBy2Key(Collection collection, Function key1, Function key2) { - if (CollUtil.isEmpty(collection)) { - return Collections.emptyMap(); - } - return collection - .stream() - .collect(Collectors.groupingBy(key1, Collectors.groupingBy(key2, Collectors.toList()))); + return groupBy2Key(collection, key1, key2, false); } @@ -176,17 +146,13 @@ public class CollStreamUtil { * @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); - } + public static Map>> groupBy2Key(Collection collection, Function key1, + Function key2, boolean isParallel) { if (CollUtil.isEmpty(collection)) { return Collections.emptyMap(); } - return collection - .parallelStream() + return StreamUtil.of(collection, isParallel) .collect(Collectors.groupingBy(key1, Collectors.groupingBy(key2, Collectors.toList()))); } @@ -203,12 +169,7 @@ public class CollStreamUtil { * @return 分类后的map */ public static Map> group2Map(Collection collection, Function key1, Function key2) { - if (CollUtil.isEmpty(collection) || key1 == null || key2 == null) { - return Collections.emptyMap(); - } - return collection - .stream() - .collect(Collectors.groupingBy(key1, Collectors.toMap(key2, Function.identity(), (l, r) -> l))); + return group2Map(collection, key1, key2, false); } /** @@ -223,17 +184,13 @@ public class CollStreamUtil { * @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); - } + public static Map> group2Map(Collection collection, + Function key1, Function key2, boolean isParallel) { if (CollUtil.isEmpty(collection) || key1 == null || key2 == null) { return Collections.emptyMap(); } - return collection - .parallelStream() + return StreamUtil.of(collection, isParallel) .collect(Collectors.groupingBy(key1, Collectors.toMap(key2, Function.identity(), (l, r) -> l))); } @@ -248,14 +205,7 @@ public class CollStreamUtil { * @return 转化后的list */ public static List toList(Collection collection, Function function) { - if (CollUtil.isEmpty(collection)) { - return Collections.emptyList(); - } - return collection - .stream() - .map(function) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + return toList(collection, function, false); } /** @@ -268,17 +218,12 @@ public class CollStreamUtil { * @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() + return StreamUtil.of(collection, isParallel) .map(function) .filter(Objects::nonNull) .collect(Collectors.toList()); @@ -295,14 +240,7 @@ public class CollStreamUtil { * @return 转化后的Set */ public static Set toSet(Collection collection, Function function) { - if (CollUtil.isEmpty(collection) || function == null) { - return Collections.emptySet(); - } - return collection - .stream() - .map(function) - .filter(Objects::nonNull) - .collect(Collectors.toSet()); + return toSet(collection, function, false); } /** @@ -315,17 +253,12 @@ public class CollStreamUtil { * @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) { + if (CollUtil.isEmpty(collection)) { return Collections.emptySet(); } - return collection - .parallelStream() + return StreamUtil.of(collection, isParallel) .map(function) .filter(Objects::nonNull) .collect(Collectors.toSet());