mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
CollUtil新增2个互换元素位置的静态方法以及对应单元测试
方法1:swapIndex 指定列表元素A与索引index所在元素互换位置 方法2:swapElement 指定列表元素A与列表元素B互换位置
This commit is contained in:
@@ -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("李四"));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user