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 080b1c419..b0d8bfe12 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 @@ -608,7 +608,7 @@ public class ListUtil { public static void swapTo(List list, T element, Integer targetIndex) { if (CollUtil.isNotEmpty(list)) { final int index = list.indexOf(element); - if (index > 0) { + if (index >= 0) { Collections.swap(list, index, targetIndex); } } @@ -627,7 +627,7 @@ public class ListUtil { public static void swapElement(List list, T element, T targetElement) { if (CollUtil.isNotEmpty(list)) { final int targetIndex = list.indexOf(targetElement); - if (targetIndex > 0) { + if (targetIndex >= 0) { swapTo(list, element, targetIndex); } } 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 5cecd5de3..937fb5917 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 @@ -227,5 +227,9 @@ public class ListUtilTest { ListUtil.swapElement(list, map2, map3); Map map = list.get(2); Assert.assertEquals("李四", map.get("2")); + + ListUtil.swapElement(list, map2, map1); + map = list.get(0); + Assert.assertEquals("李四", map.get("2")); } }