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.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
@ -748,4 +749,46 @@ public class ListUtil {
|
||||
}
|
||||
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.Data;
|
||||
import org.dromara.hutool.core.convert.Convert;
|
||||
import org.dromara.hutool.core.date.StopWatch;
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.core.lang.page.PageInfo;
|
||||
@ -78,7 +79,7 @@ public class ListUtilTest {
|
||||
|
||||
@Test
|
||||
public void splitAvgNotZero() {
|
||||
Assertions.assertThrows(IllegalArgumentException.class, ()->{
|
||||
Assertions.assertThrows(IllegalArgumentException.class, () -> {
|
||||
// limit不能小于等于0
|
||||
ListUtil.avgPartition(Arrays.asList(1, 2, 3, 4), 0);
|
||||
});
|
||||
@ -122,7 +123,7 @@ public class ListUtilTest {
|
||||
public void pageTest2() {
|
||||
final List<Integer> a = ListUtil.ofLinked(1, 2, 3, 4, 5);
|
||||
final int[] d1 = ListUtil.page(a, PageInfo.of(a.size(), 8).setFirstPageNo(0).setPageNo(0))
|
||||
.stream().mapToInt(Integer::valueOf).toArray();
|
||||
.stream().mapToInt(Integer::valueOf).toArray();
|
||||
Assertions.assertArrayEquals(new int[]{1, 2, 3, 4, 5}, d1);
|
||||
}
|
||||
|
||||
@ -170,11 +171,11 @@ public class ListUtilTest {
|
||||
}
|
||||
|
||||
final List<TestBean> beanList = ListUtil.of(
|
||||
new TestBean(2, "test2"),
|
||||
new TestBean(1, "test1"),
|
||||
new TestBean(5, "test5"),
|
||||
new TestBean(4, "test4"),
|
||||
new TestBean(3, "test3")
|
||||
new TestBean(2, "test2"),
|
||||
new TestBean(1, "test1"),
|
||||
new TestBean(5, "test5"),
|
||||
new TestBean(4, "test4"),
|
||||
new TestBean(3, "test3")
|
||||
);
|
||||
|
||||
final List<TestBean> order = ListUtil.sortByProperty(beanList, "order");
|
||||
@ -245,13 +246,13 @@ public class ListUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofCopyOnWriteTest(){
|
||||
public void ofCopyOnWriteTest() {
|
||||
final CopyOnWriteArrayList<String> strings = ListUtil.ofCopyOnWrite(ListUtil.of("a", "b"));
|
||||
Assertions.assertEquals(2, strings.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofCopyOnWriteTest2(){
|
||||
public void ofCopyOnWriteTest2() {
|
||||
final CopyOnWriteArrayList<String> strings = ListUtil.ofCopyOnWrite("a", "b");
|
||||
Assertions.assertEquals(2, strings.size());
|
||||
}
|
||||
@ -269,4 +270,22 @@ public class ListUtilTest {
|
||||
|
||||
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