From b1337edc30ab5532710133d46cf70ff32f473c00 Mon Sep 17 00:00:00 2001
From: Liang Long <54536224+GRain-long@users.noreply.github.com>
Date: Tue, 23 Jun 2020 18:41:14 +0800
Subject: [PATCH 1/2] add intersectionDistinct
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加交集去重方法
---
.../cn/hutool/core/collection/CollUtil.java | 33 +++++++++++++++++++
1 file changed, 33 insertions(+)
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 f11f25171..576adabe6 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
@@ -266,6 +266,39 @@ public class CollUtil {
}
return intersection;
}
+
+ /**
+ * 多个集合的交集
+ * 针对一个集合中存在多个相同元素的情况,只保留一个
+ * 例如:集合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 intersectionDistinct(Collection coll1, Collection coll2, Collection... otherColls) {
+ final Set result;
+ if (isEmpty(coll1)) {
+ result = new LinkedHashSet<>();
+ } else {
+ result = new LinkedHashSet<>(coll1);
+ }
+
+ if (isNotEmpty(coll2)) {
+ result.retainAll(coll2);
+ }
+
+ if (ArrayUtil.isNotEmpty(otherColls)) {
+ for (Collection otherColl : otherColls) {
+ result.retainAll(otherColl);
+ }
+ }
+ return result;
+ }
/**
* 两个集合的差集
From 1bc2fe138e0013c39e2fa2c5559d861ec6245685 Mon Sep 17 00:00:00 2001
From: Liang Long <54536224+GRain-long@users.noreply.github.com>
Date: Tue, 23 Jun 2020 18:45:52 +0800
Subject: [PATCH 2/2] add intersectionDistinctTerst
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加交集去重测试方法
---
.../cn/hutool/core/collection/CollUtilTest.java | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)
diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java
index c3b98bd01..e249602f1 100644
--- a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java
+++ b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java
@@ -76,8 +76,21 @@ public class CollUtilTest {
ArrayList list1 = CollUtil.newArrayList("a", "b", "b", "c", "d", "x");
ArrayList list2 = CollUtil.newArrayList("a", "b", "b", "b", "c", "d");
- Collection union = CollUtil.intersection(list1, list2);
- Assert.assertEquals(2, CollUtil.count(union, t -> t.equals("b")));
+ Collection intersection = CollUtil.intersection(list1, list2);
+ Assert.assertEquals(2, CollUtil.count(intersection, t -> t.equals("b")));
+ }
+
+ @Test
+ public void intersectionTest2() {
+ ArrayList list1 = CollUtil.newArrayList("a", "b", "b", "c", "d", "x");
+ ArrayList list2 = CollUtil.newArrayList("a", "b", "b", "b", "c", "d");
+ ArrayList list3 = CollUtil.newArrayList();
+
+ Collection intersectionDistinct = CollUtil.intersectionDistinct(list1, list2);
+ Assert.assertEquals(CollUtil.newLinkedHashSet("a", "b", "c", "d"), intersectionDistinct);
+
+ Collection intersectionDistinct2 = CollUtil.intersectionDistinct(list1, list2, list3);
+ Assert.assertTrue(intersectionDistinct2.isEmpty());
}
@Test