mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
Merge pull request #2144 from seaswalker/fix_swap_element
Fix swap element
This commit is contained in:
commit
10f65045e3
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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"));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user