diff --git a/CHANGELOG.md b/CHANGELOG.md index 584f5769b..95a3a6968 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ * 【core 】 CsvWriter的write和writeBeans参数改为Iterable(issue#I49O4S@Gitee) * 【core 】 BitStatusUtil添加来源声明(issue#1824@Github) * 【core 】 UrlQuery.build增加重载,支持可选是否转义(issue#I4AIX1@Gitee) +* 【core 】 ListUtil增加swapTo和swapElement方法(pr#416@Gitee) * ### 🐞Bug修复 * 【core 】 修复FuncKey函数无效问题 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 682645f52..2083ced3d 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,34 +2954,4 @@ 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/main/java/cn/hutool/core/collection/ListUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java index fda860acd..4002f22f2 100644 --- a/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java @@ -312,7 +312,7 @@ public class ListUtil { * @see Collections#sort(List, Comparator) */ public static List sort(List list, Comparator c) { - if(CollUtil.isEmpty(list)){ + if (CollUtil.isEmpty(list)) { return list; } list.sort(c); @@ -593,4 +593,41 @@ public class ListUtil { ? new RandomAccessAvgPartition<>(list, limit) : new AvgPartition<>(list, limit); } + + /** + * 将指定元素交换到指定索引位置,其他元素的索引值不变
+ * 交换会修改原List
+ * 如果集合中有多个相同元素,只交换第一个找到的元素 + * + * @param list 列表 + * @param element 需交换元素 + * @param targetIndex 目标索引 + * @since 5.7.13 + */ + public static void swapTo(List list, T element, Integer targetIndex) { + if (CollUtil.isNotEmpty(list)) { + final int index = list.indexOf(element); + if (index > 0) { + Collections.swap(list, index, targetIndex); + } + } + } + + /** + * 将指定元素交换到指定元素位置,其他元素的索引值不变
+ * 交换会修改原List
+ * 如果集合中有多个相同元素,只交换第一个找到的元素 + * + * @param list 列表 + * @param element 需交换元素 + * @param targetElement 目标元素 + */ + public static void swapElement(List list, T element, T targetElement) { + if (CollUtil.isNotEmpty(list)) { + final int targetIndex = list.indexOf(targetElement); + if (targetIndex > 0) { + swapTo(list, element, targetIndex); + } + } + } } 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 a296af545..80c66af8f 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 @@ -772,25 +772,4 @@ public class CollUtilTest { 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("李四")); - } } diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java index 5048de097..912df5904 100644 --- a/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java @@ -12,7 +12,9 @@ import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class ListUtilTest { @@ -202,4 +204,25 @@ public class ListUtilTest { Assert.assertEquals("test4", order.get(3).getName()); Assert.assertEquals("test5", order.get(4).getName()); } + + @Test + public void swapIndex() { + List list = Arrays.asList(7, 2, 8, 9); + ListUtil.swapTo(list, 8, 1); + Assert.assertEquals(8, (int) list.get(1)); + } + + @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); + ListUtil.swapElement(list, map2, map3); + Map map = list.get(2); + Assert.assertEquals("李四", map.get("2")); + } }