From c047fd5a239a804e42dbe8f8fd77ccc68809d320 Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 13 May 2020 04:58:40 +0800 Subject: [PATCH] add method and test --- CHANGELOG.md | 3 +- .../copier/provider/MapValueProvider.java | 2 +- .../cn/hutool/core/collection/CollUtil.java | 68 +++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1f9cf71b..c84e852e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,12 +3,13 @@ ------------------------------------------------------------------------------------------------------------- -## 5.3.5 (2020-05-11) +## 5.3.5 (2020-05-13) ### 新特性 * 【core 】 增加CollUtil.map方法 * 【extra 】 增加Sftp.lsEntries方法,Ftp和Sftp增加recursiveDownloadFolder(pr#121@Gitee) * 【system 】 OshiUtil增加getNetworkIFs方法 +* 【core 】 CollUtil增加unionDistinct、unionAll方法(pr#122@Gitee) ### Bug修复 ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/bean/copier/provider/MapValueProvider.java b/hutool-core/src/main/java/cn/hutool/core/bean/copier/provider/MapValueProvider.java index 97275f3b6..2007776b6 100644 --- a/hutool-core/src/main/java/cn/hutool/core/bean/copier/provider/MapValueProvider.java +++ b/hutool-core/src/main/java/cn/hutool/core/bean/copier/provider/MapValueProvider.java @@ -61,10 +61,10 @@ public class MapValueProvider implements ValueProvider { @Override public boolean containsKey(String key) { - //检查下划线模式 if(map.containsKey(key)) { return true; }else { + //检查下划线模式 return map.containsKey(StrUtil.toUnderlineCase(key)); } } diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java index a4ccaf888..ec3c5437a 100644 --- a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java @@ -144,6 +144,74 @@ public class CollUtil { return union; } + /** + * 多个集合的非重复并集,类似于SQL中的“UNION DISTINCT”
+ * 针对一个集合中存在多个相同元素的情况,只保留一个
+ * 例如:集合1:[a, b, c, c, c],集合2:[a, b, c, c]
+ * 结果:[a, b, c],此结果中只保留了一个c + * + * @param 集合元素类型 + * @param coll1 集合1 + * @param coll2 集合2 + * @param otherColls 其它集合 + * @return 并集的集合,返回 {@link LinkedHashSet} + */ + @SafeVarargs + public static Set unionDistinct(Collection coll1, Collection coll2, Collection... otherColls) { + final Set result; + if(isEmpty(coll1)){ + result = new LinkedHashSet<>(); + } else { + result = new LinkedHashSet<>(coll1); + } + + if(isNotEmpty(coll2)){ + result.addAll(coll2); + } + + if(ArrayUtil.isNotEmpty(otherColls)){ + for (Collection otherColl : otherColls) { + result.addAll(otherColl); + } + } + + return result; + } + + /** + * 多个集合的完全并集,类似于SQL中的“UNION ALL”
+ * 针对一个集合中存在多个相同元素的情况,保留全部元素
+ * 例如:集合1:[a, b, c, c, c],集合2:[a, b, c, c]
+ * 结果:[a, b, c, c, c, a, b, c, c] + * + * @param 集合元素类型 + * @param coll1 集合1 + * @param coll2 集合2 + * @param otherColls 其它集合 + * @return 并集的集合,返回 {@link ArrayList} + */ + @SafeVarargs + public static List unionAll(Collection coll1, Collection coll2, Collection... otherColls) { + final List result; + if(isEmpty(coll1)){ + result = new ArrayList<>(); + } else { + result = new ArrayList<>(coll1); + } + + if(isNotEmpty(coll2)){ + result.addAll(coll2); + } + + if(ArrayUtil.isNotEmpty(otherColls)){ + for (Collection otherColl : otherColls) { + result.addAll(otherColl); + } + } + + return result; + } + /** * 两个集合的交集
* 针对一个集合中存在多个相同元素的情况,计算两个集合中此元素的个数,保留最少的个数