mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-03 20:27:58 +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) {
|
public static <T> void swapTo(List<T> list, T element, Integer targetIndex) {
|
||||||
if (CollUtil.isNotEmpty(list)) {
|
if (CollUtil.isNotEmpty(list)) {
|
||||||
final int index = list.indexOf(element);
|
final int index = list.indexOf(element);
|
||||||
if (index > 0) {
|
if (index >= 0) {
|
||||||
Collections.swap(list, index, targetIndex);
|
Collections.swap(list, index, targetIndex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -627,7 +627,7 @@ public class ListUtil {
|
|||||||
public static <T> void swapElement(List<T> list, T element, T targetElement) {
|
public static <T> void swapElement(List<T> list, T element, T targetElement) {
|
||||||
if (CollUtil.isNotEmpty(list)) {
|
if (CollUtil.isNotEmpty(list)) {
|
||||||
final int targetIndex = list.indexOf(targetElement);
|
final int targetIndex = list.indexOf(targetElement);
|
||||||
if (targetIndex > 0) {
|
if (targetIndex >= 0) {
|
||||||
swapTo(list, element, targetIndex);
|
swapTo(list, element, targetIndex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -227,5 +227,9 @@ public class ListUtilTest {
|
|||||||
ListUtil.swapElement(list, map2, map3);
|
ListUtil.swapElement(list, map2, map3);
|
||||||
Map<String, String> map = list.get(2);
|
Map<String, String> map = list.get(2);
|
||||||
Assert.assertEquals("李四", map.get("2"));
|
Assert.assertEquals("李四", map.get("2"));
|
||||||
|
|
||||||
|
ListUtil.swapElement(list, map2, map1);
|
||||||
|
map = list.get(0);
|
||||||
|
Assert.assertEquals("李四", map.get("2"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user