Skip to content

Commit

Permalink
add method
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Sep 17, 2021
1 parent 2f57c2a commit b935ace
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* 【core 】 CsvWriter的write和writeBeans参数改为Iterable(issue#I49O4S@Gitee)
* 【core 】 BitStatusUtil添加来源声明(issue#1824@Github)
* 【core 】 UrlQuery.build增加重载,支持可选是否转义(issue#I4AIX1@Gitee)
* 【core 】 ListUtil增加swapTo和swapElement方法(pr#416@Gitee)
*
### 🐞Bug修复
* 【core 】 修复FuncKey函数无效问题
Expand Down
30 changes: 0 additions & 30 deletions hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2954,34 +2954,4 @@ public static boolean isEqualList(final Collection<?> list1, final Collection<?>

return IterUtil.isEqualList(list1, list2);
}

/**
* 将指定元素交换到指定索引位置,其他元素的索引值不变
* 交换会修改原List
*
* @param list 列表
* @param element 需交换元素
* @param targetIndex 目标索引
*/
public static <T> void swapIndex(List<T> list, T element, Integer targetIndex) {
if (isEmpty(list) || !list.contains(element)) {
return;
}
Collections.swap(list, list.indexOf(element), targetIndex);
}

/**
* 将指定元素交换到指定元素位置,其他元素的索引值不变
* 交换会修改原List
*
* @param list 列表
* @param element 需交换元素
* @param targetElement 目标元素
*/
public static <T> void swapElement(List<T> list, T element, T targetElement) {
if (isEmpty(list) || !list.contains(targetElement)) {
return;
}
swapIndex(list, element, list.indexOf(targetElement));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public static <T> void page(List<T> list, int pageSize, Consumer<List<T>> pageLi
* @see Collections#sort(List, Comparator)
*/
public static <T> List<T> sort(List<T> list, Comparator<? super T> c) {
if(CollUtil.isEmpty(list)){
if (CollUtil.isEmpty(list)) {
return list;
}
list.sort(c);
Expand Down Expand Up @@ -593,4 +593,41 @@ public static <T> List<List<T>> splitAvg(List<T> list, int limit) {
? new RandomAccessAvgPartition<>(list, limit)
: new AvgPartition<>(list, limit);
}

/**
* 将指定元素交换到指定索引位置,其他元素的索引值不变<br>
* 交换会修改原List<br>
* 如果集合中有多个相同元素,只交换第一个找到的元素
*
* @param list 列表
* @param element 需交换元素
* @param targetIndex 目标索引
* @since 5.7.13
*/
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) {
Collections.swap(list, index, targetIndex);
}
}
}

/**
* 将指定元素交换到指定元素位置,其他元素的索引值不变<br>
* 交换会修改原List<br>
* 如果集合中有多个相同元素,只交换第一个找到的元素
*
* @param list 列表
* @param element 需交换元素
* @param targetElement 目标元素
*/
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) {
swapTo(list, element, targetIndex);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -772,25 +772,4 @@ public void sortComparableTest() {
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("李四"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListUtilTest {

Expand Down Expand Up @@ -202,4 +204,25 @@ class TestBean{
Assert.assertEquals("test4", order.get(3).getName());
Assert.assertEquals("test5", order.get(4).getName());
}

@Test
public void swapIndex() {
List<Integer> list = Arrays.asList(7, 2, 8, 9);
ListUtil.swapTo(list, 8, 1);
Assert.assertEquals(8, (int) list.get(1));
}

@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);
ListUtil.swapElement(list, map2, map3);
Map<String, String> map = list.get(2);
Assert.assertEquals("李四", map.get("2"));
}
}

0 comments on commit b935ace

Please sign in to comment.