From 3fc4d134aff963db7ea9fdc7ad141cf9210d3186 Mon Sep 17 00:00:00 2001 From: TLH <2371644177@qq.com> Date: Wed, 15 Sep 2021 16:02:20 +0800 Subject: [PATCH] =?UTF-8?q?CollUtil=E6=96=B0=E5=A2=9E2=E4=B8=AA=E4=BA=92?= =?UTF-8?q?=E6=8D=A2=E5=85=83=E7=B4=A0=E4=BD=8D=E7=BD=AE=E7=9A=84=E9=9D=99?= =?UTF-8?q?=E6=80=81=E6=96=B9=E6=B3=95=E4=BB=A5=E5=8F=8A=E5=AF=B9=E5=BA=94?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=20=E6=96=B9=E6=B3=951:swap?= =?UTF-8?q?Index=20=20=20=E6=8C=87=E5=AE=9A=E5=88=97=E8=A1=A8=E5=85=83?= =?UTF-8?q?=E7=B4=A0A=E4=B8=8E=E7=B4=A2=E5=BC=95index=E6=89=80=E5=9C=A8?= =?UTF-8?q?=E5=85=83=E7=B4=A0=E4=BA=92=E6=8D=A2=E4=BD=8D=E7=BD=AE=20?= =?UTF-8?q?=E6=96=B9=E6=B3=952:swapElement=20=E6=8C=87=E5=AE=9A=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E5=85=83=E7=B4=A0A=E4=B8=8E=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E5=85=83=E7=B4=A0B=E4=BA=92=E6=8D=A2=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/collection/CollUtil.java | 30 +++++++++++++++++++ .../hutool/core/collection/CollUtilTest.java | 23 +++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) 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 2083ced3d..682645f52 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 @@ -2954,4 +2954,34 @@ public class CollUtil { return IterUtil.isEqualList(list1, list2); } + + /** + * 将指定元素交换到指定索引位置,其他元素的索引值不变 + * 交换会修改原List + * + * @param list 列表 + * @param element 需交换元素 + * @param targetIndex 目标索引 + */ + public static void swapIndex(List list, T element, Integer targetIndex) { + if (isEmpty(list) || !list.contains(element)) { + return; + } + Collections.swap(list, list.indexOf(element), targetIndex); + } + + /** + * 将指定元素交换到指定元素位置,其他元素的索引值不变 + * 交换会修改原List + * + * @param list 列表 + * @param element 需交换元素 + * @param targetElement 目标元素 + */ + public static void swapElement(List list, T element, T targetElement) { + if (isEmpty(list) || !list.contains(targetElement)) { + return; + } + swapIndex(list, element, list.indexOf(targetElement)); + } } 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 4d8717dcb..a296af545 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 @@ -767,9 +767,30 @@ public class CollUtilTest { } @Test - public void sortComparableTest(){ + public void sortComparableTest() { final List of = ListUtil.toList("a", "c", "b"); final List sort = CollUtil.sort(of, new ComparableComparator<>()); Assert.assertEquals("a,b,c", CollUtil.join(sort, ",")); } + + @Test + public void swapIndex() { + List list = Arrays.asList(7, 2, 8, 9); + CollUtil.swapIndex(list, 8, 1); + Assert.assertTrue(list.get(1) == 8); + } + + @Test + public void swapElement() { + Map map1 = new HashMap<>(); + map1.put("1", "张三"); + Map map2 = new HashMap<>(); + map2.put("2", "李四"); + Map map3 = new HashMap<>(); + map3.put("3", "王五"); + List> list = Arrays.asList(map1, map2, map3); + CollUtil.swapElement(list, map2, map3); + Map map = list.get(2); + Assert.assertTrue(map.get("2").equals("李四")); + } }