From 3993aa1ea1b556afbf88a73a2ff4b18a091200fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=9C=E5=87=BD=E6=95=8F?= Date: Tue, 7 Apr 2020 14:14:00 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=95=B0=E5=AD=A6=E9=9B=86=E5=90=88?= =?UTF-8?q?=E8=BF=90=E7=AE=97<=E4=BA=A4=E3=80=81=E5=B7=AE=E3=80=81?= =?UTF-8?q?=E5=B9=B6>?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/math/OperationsUtil.java | 215 ++++++++++++++++++ 1 file changed, 215 insertions(+) create mode 100644 hutool-core/src/main/java/cn/hutool/core/math/OperationsUtil.java diff --git a/hutool-core/src/main/java/cn/hutool/core/math/OperationsUtil.java b/hutool-core/src/main/java/cn/hutool/core/math/OperationsUtil.java new file mode 100644 index 0000000000..1176bdb49b --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/math/OperationsUtil.java @@ -0,0 +1,215 @@ +package cn.hutool.core.math; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * 数学集合运算<交、差、并> + * @author 野鹿 + */ +public class OperationsUtil { + + public OperationsUtil() { + } + + /** + * 求交集 + * + * @param str + * @param small + * @return + */ + public static List lntersection(String str, List small) { + List big = new ArrayList(); + big.add(str); + Set result = new HashSet<>(); + result.addAll(small); + result.retainAll(big); + return new ArrayList<>(result); + } + + /** + * 求交集两个list + * + * @param big + * @param small + * @return + */ + public static List lntersection(List big, List small) { + Set result = new HashSet<>(); + result.addAll(big); + result.retainAll(small); + return new ArrayList<>(result); + } + /** + * 求交集map和list + * + * @param mapbig + * @param small + * @return + */ + public static List lntersection(Map mapbig, List small) { + Set result = new HashSet<>(); + result.addAll(mapbig.keySet()); + result.retainAll(small); + return new ArrayList<>(result); + } + /** + * 求交集map和list + * + * @param big + * @param mapsmall + * @return + */ + public static List lntersection(List big, Map mapsmall) { + Set result = new HashSet<>(); + result.addAll(big); + result.retainAll(mapsmall.keySet()); + return new ArrayList<>(result); + } + + /** + * 求交集map和map + * + * @param mapbig + * @param mapsmall + * @return + */ + public static List lntersection(Map mapbig, Map mapsmall) { + Set result = new HashSet<>(); + result.addAll(mapbig.keySet()); + result.retainAll(mapsmall.keySet()); + return new ArrayList<>(result); + } + + + /** + * 求差集list和list + * + * @param big + * @param small + * @return + */ + public static List difference(List big,List small) { + Set result = new HashSet<>(); + result.addAll(big); + result.removeAll(small); + return new ArrayList<>(result); + } + + /** + * 求差集list和list + * + * @param big + * @param small + * @return + */ + public static List difference(Set big,Set small) { + Set result = new HashSet<>(); + result.addAll(big); + result.removeAll(small); + return new ArrayList<>(result); + } + /** + * 求差集map和list + * + * @param mapbig + * @param small + * @return + */ + public static List difference(Map mapbig,List small) { + Set result = new HashSet<>(); + result.addAll(mapbig.keySet()); + result.removeAll(small); + return new ArrayList<>(result); + } + + /** + * 求差集list和map + * + * @param big + * @param mapsmall + * @return + */ + public static List difference(List big, Map mapsmall) { + Set result = new HashSet<>(); + result.addAll(big); + result.removeAll(mapsmall.keySet()); + return new ArrayList<>(result); + } + + /** + * 求差集map和map + * + * @param mapbig + * @param mapsmall + * @return + */ + public static List difference(Map mapbig, Map mapsmall) { + Set result = new HashSet<>(); + result.addAll(mapbig.keySet()); + result.removeAll(mapsmall.keySet()); + return new ArrayList<>(result); + } + + /** + * 求并集list和list + * + * @param big + * @param small + * @return + */ + public static List union(List big, List small) { + Set result = new HashSet<>(); + result.addAll(big); + result.addAll(small); + return new ArrayList<>(result); + } + + /** + * 求并集map和list + * + * @param mapbig + * @param small + * @return + */ + public static List union(Map mapbig, List small) { + Set result = new HashSet<>(); + result.addAll(mapbig.keySet()); + result.addAll(small); + return new ArrayList<>(result); + } + + /** + * 求并集map和list + * + * @param big + * @param mapsmall + * @return + */ + public static List union(List big, Map mapsmall) { + Set result = new HashSet<>(); + result.addAll(big); + result.addAll(mapsmall.keySet()); + return new ArrayList<>(result); + } + + /** + * 求并集map和map + * + * @param mapbig + * @param mapsmall + * @return + */ + public static List union(Map mapbig, Map mapsmall) { + Set result = new HashSet<>(); + result.addAll(mapbig.keySet()); + result.addAll(mapsmall.keySet()); + return new ArrayList<>(result); + } + +} + -- Gitee From eb55d85f1973971debd8dd1457be891068dfe457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=9C=E5=87=BD=E6=95=8F?= Date: Tue, 7 Apr 2020 14:26:49 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=95=B0=E5=AD=A6=E9=9B=86=E5=90=88?= =?UTF-8?q?=E8=BF=90=E7=AE=97<=E4=BA=A4=E3=80=81=E5=B7=AE=E3=80=81?= =?UTF-8?q?=E5=B9=B6>=20=20=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/math/OperationsTset.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 hutool-core/src/test/java/cn/hutool/core/math/OperationsTset.java diff --git a/hutool-core/src/test/java/cn/hutool/core/math/OperationsTset.java b/hutool-core/src/test/java/cn/hutool/core/math/OperationsTset.java new file mode 100644 index 0000000000..53d836e150 --- /dev/null +++ b/hutool-core/src/test/java/cn/hutool/core/math/OperationsTset.java @@ -0,0 +1,48 @@ +/** + * *@Copyright: 2019 www.pingshiedu.tech inc . All rights reserved. + * *注意:本内容仅限于评师教育内部传阅,禁止外泄以及用于其他的商业目 + */ +package cn.hutool.core.math; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +/** + * 排列单元测试 + * @author 野鹿(duhanmin@foxmail.com) + * + */ +public class OperationsTset { + @Test + public void lntersection() { + List big = Arrays.asList("1","2"); + List small = Arrays.asList("1","2","3"); + + List list = OperationsUtil.lntersection(big,small); + System.out.println(list); + Assert.assertEquals(Arrays.asList("1","2"), list); + } + + @Test + public void difference() { + List big = Arrays.asList("1","2","3"); + List small = Arrays.asList("1","2"); + List list = OperationsUtil.difference(big,small); + System.out.println(list); + Assert.assertEquals(Arrays.asList("3"), list); + } + + @Test + public void union() { + List big = Arrays.asList("1","2","3"); + List small = Arrays.asList("1","4"); + List list = OperationsUtil.union(big,small); + System.out.println(list); + Assert.assertEquals(Arrays.asList("1","2","3","4"), list); + } + + +} -- Gitee From ea9c143bf4ae5822db60ce8037579f140fde953d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=9C=E5=87=BD=E6=95=8F?= Date: Tue, 7 Apr 2020 14:35:13 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=86=97=E4=BD=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/test/java/cn/hutool/core/math/OperationsTset.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/hutool-core/src/test/java/cn/hutool/core/math/OperationsTset.java b/hutool-core/src/test/java/cn/hutool/core/math/OperationsTset.java index 53d836e150..d1315be0a4 100644 --- a/hutool-core/src/test/java/cn/hutool/core/math/OperationsTset.java +++ b/hutool-core/src/test/java/cn/hutool/core/math/OperationsTset.java @@ -1,7 +1,3 @@ -/** - * *@Copyright: 2019 www.pingshiedu.tech inc . All rights reserved. - * *注意:本内容仅限于评师教育内部传阅,禁止外泄以及用于其他的商业目 - */ package cn.hutool.core.math; import org.junit.Assert; -- Gitee