Merge pull request #2144 from seaswalker/fix_swap_element

Fix swap element
This commit is contained in:
Golden Looly 2022-02-17 09:29:43 +08:00 committed by GitHub
commit 10f65045e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 2 deletions

View File

@ -608,7 +608,7 @@ public class ListUtil {
public static <T> void swapTo(List<T> 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 <T> void swapElement(List<T> 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);
}
}

View File

@ -227,5 +227,9 @@ public class ListUtilTest {
ListUtil.swapElement(list, map2, map3);
Map<String, String> map = list.get(2);
Assert.assertEquals("李四", map.get("2"));
ListUtil.swapElement(list, map2, map1);
map = list.get(0);
Assert.assertEquals("李四", map.get("2"));
}
}