CollUtil新增2个互换元素位置的静态方法以及对应单元测试

方法1:swapIndex   指定列表元素A与索引index所在元素互换位置
方法2:swapElement 指定列表元素A与列表元素B互换位置
This commit is contained in:
TLH
2021-09-15 16:02:20 +08:00
parent 8e2a1ab1ef
commit 3fc4d134af
2 changed files with 52 additions and 1 deletions

View File

@@ -767,9 +767,30 @@ public class CollUtilTest {
}
@Test
public void sortComparableTest(){
public void sortComparableTest() {
final List<String> of = ListUtil.toList("a", "c", "b");
final List<String> sort = CollUtil.sort(of, new ComparableComparator<>());
Assert.assertEquals("a,b,c", CollUtil.join(sort, ","));
}
@Test
public void swapIndex() {
List<Integer> list = Arrays.asList(7, 2, 8, 9);
CollUtil.swapIndex(list, 8, 1);
Assert.assertTrue(list.get(1) == 8);
}
@Test
public void swapElement() {
Map<String, String> map1 = new HashMap<>();
map1.put("1", "张三");
Map<String, String> map2 = new HashMap<>();
map2.put("2", "李四");
Map<String, String> map3 = new HashMap<>();
map3.put("3", "王五");
List<Map<String, String>> list = Arrays.asList(map1, map2, map3);
CollUtil.swapElement(list, map2, map3);
Map<String, String> map = list.get(2);
Assert.assertTrue(map.get("2").equals("李四"));
}
}