mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
ListUtil添加解构list内部的多个list为单个list的方法
This commit is contained in:
parent
095507a86d
commit
7cb8492f18
@ -27,6 +27,7 @@ import org.dromara.hutool.core.util.ObjUtil;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Function;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -748,4 +749,46 @@ public class ListUtil {
|
|||||||
}
|
}
|
||||||
return resList;
|
return resList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解构list里面的list为单个list
|
||||||
|
*
|
||||||
|
* @param list 传入的list集合
|
||||||
|
* @param <T> 返回的元素类型
|
||||||
|
* @return 解构后的list集合
|
||||||
|
*/
|
||||||
|
public static <T> List<T> flatList(List<?> list) {
|
||||||
|
return flatList(list, Function.identity());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解构list里面的list,并可以对每个元素操作
|
||||||
|
*
|
||||||
|
* @param list 传入的list集合
|
||||||
|
* @param operation 对每个元素进行操作
|
||||||
|
* @param <T> 返回的元素类型
|
||||||
|
* @param <O> 最内侧的元素类型
|
||||||
|
* @return 解构后的list集合
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("all")
|
||||||
|
public static <T, O> List<T> flatList(List<?> list, Function<O, T> operation) {
|
||||||
|
List<T> result = new ArrayList<>();
|
||||||
|
|
||||||
|
if (list == null || list.isEmpty()) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (list.get(0) instanceof List) {
|
||||||
|
for (List<?> subList : (List<List<?>>) list) {
|
||||||
|
result.addAll(flatList(subList, operation));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (Object item : list) {
|
||||||
|
result.add(operation.apply((O) item));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package org.dromara.hutool.core.collection;
|
|||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import org.dromara.hutool.core.convert.Convert;
|
||||||
import org.dromara.hutool.core.date.StopWatch;
|
import org.dromara.hutool.core.date.StopWatch;
|
||||||
import org.dromara.hutool.core.lang.Console;
|
import org.dromara.hutool.core.lang.Console;
|
||||||
import org.dromara.hutool.core.lang.page.PageInfo;
|
import org.dromara.hutool.core.lang.page.PageInfo;
|
||||||
@ -269,4 +270,22 @@ public class ListUtilTest {
|
|||||||
|
|
||||||
ListUtil.reverseNew(list);
|
ListUtil.reverseNew(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void flatListTest1() {
|
||||||
|
List<List<List<String>>> list = Arrays.asList(Arrays.asList(Arrays.asList("1", "2", "3"), Arrays.asList("5", "6", "7")));
|
||||||
|
|
||||||
|
List<String> objects = ListUtil.flatList(list);
|
||||||
|
|
||||||
|
Assertions.assertArrayEquals(new String[]{"1", "2", "3", "5", "6", "7"}, objects.toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void flatListTest2() {
|
||||||
|
List<List<List<String>>> list = Arrays.asList(Arrays.asList(Arrays.asList("1", "2", "3"), Arrays.asList("5", "6", "7")));
|
||||||
|
|
||||||
|
List<Integer> objects = ListUtil.flatList(list, Convert::toInt);
|
||||||
|
Assertions.assertArrayEquals(new Integer[]{1, 2, 3, 5, 6, 7}, objects.toArray());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user