mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add method
This commit is contained in:
parent
2f57c2aaf9
commit
b935ace121
@ -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函数无效问题
|
||||
|
@ -2954,34 +2954,4 @@ public class CollUtil {
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
@ -312,7 +312,7 @@ public class ListUtil {
|
||||
* @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);
|
||||
@ -593,4 +593,41 @@ public class ListUtil {
|
||||
? 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -772,25 +772,4 @@ public class CollUtilTest {
|
||||
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("李四"));
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,9 @@ import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ListUtilTest {
|
||||
|
||||
@ -202,4 +204,25 @@ public class ListUtilTest {
|
||||
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"));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user