mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
CollUtil添加解构collection内部的多个collection为单个list的方法
This commit is contained in:
parent
7cb8492f18
commit
4e047e98e3
@ -34,6 +34,7 @@ import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.core.reflect.ConstructorUtil;
|
||||
import org.dromara.hutool.core.reflect.FieldUtil;
|
||||
import org.dromara.hutool.core.reflect.TypeUtil;
|
||||
import org.dromara.hutool.core.stream.EasyStream;
|
||||
import org.dromara.hutool.core.stream.StreamUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.text.split.SplitUtil;
|
||||
@ -2354,4 +2355,48 @@ public class CollUtil {
|
||||
}
|
||||
return collection.stream().allMatch(predicate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 结构多层集合
|
||||
* 例如:List<List<List<String>>> 解构成 List<String>
|
||||
*
|
||||
* @param collection 需要解构的集合
|
||||
* @return 解构后的集合
|
||||
*/
|
||||
public static List<Object> flat(Collection<?> collection) {
|
||||
return flat(collection, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 结构多层集合
|
||||
* 例如:List<List<List<String>>> 解构成 List<String>
|
||||
* <p>
|
||||
* skipNull的作用是当集合里面有个值为空,当为true是解构后的集合里面没有null值,如果为false则会在解构后的集合里面有努力了。
|
||||
*
|
||||
* @param collection 需要结构的集合
|
||||
* @param skipNull 是否跳过空的值
|
||||
* @return 解构后的集合
|
||||
*/
|
||||
@SuppressWarnings({"unchecked"})
|
||||
public static List<Object> flat(Collection<?> collection, boolean skipNull) {
|
||||
LinkedList queue = EasyStream.of(collection)
|
||||
.collect(Collectors.toCollection(LinkedList::new));
|
||||
|
||||
List<Object> result = new ArrayList<>();
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
Object t = queue.removeFirst();
|
||||
|
||||
if (skipNull && t == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (t instanceof Collection) {
|
||||
queue.addAll((Collection<?>) t);
|
||||
} else {
|
||||
result.add(t);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -27,7 +27,6 @@ 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;
|
||||
|
||||
/**
|
||||
@ -749,46 +748,4 @@ 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package org.dromara.hutool.core.collection;
|
||||
import org.dromara.hutool.core.collection.iter.IterUtil;
|
||||
import org.dromara.hutool.core.collection.set.SetUtil;
|
||||
import org.dromara.hutool.core.comparator.CompareUtil;
|
||||
import org.dromara.hutool.core.convert.Convert;
|
||||
import org.dromara.hutool.core.date.DateUtil;
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.core.map.Dict;
|
||||
@ -1181,4 +1182,51 @@ public class CollUtilTest {
|
||||
public void minNullTest() {
|
||||
Assertions.assertNull(CollUtil.max(null));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void flatListTest1() {
|
||||
List<List<List<String>>> list = Arrays.asList(Arrays.asList(Arrays.asList("1", "2", "3"), Arrays.asList("5", "6", "7")));
|
||||
|
||||
List<Object> objects = CollUtil.flat(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("a"),
|
||||
Arrays.asList("b", "c"),
|
||||
Arrays.asList("d", "e", "f")
|
||||
),
|
||||
Arrays.asList(
|
||||
Arrays.asList("g", "h", "i"),
|
||||
Arrays.asList("j", "k", "l")
|
||||
)
|
||||
);
|
||||
List<Object> flat = CollUtil.flat(list);
|
||||
Assertions.assertArrayEquals(new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"}, flat.toArray());
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void flatListTest3() {
|
||||
List<List<List<String>>> list = Arrays.asList(
|
||||
Arrays.asList(
|
||||
Arrays.asList("a"),
|
||||
Arrays.asList("b", "c", null),
|
||||
Arrays.asList("d", "e", "f")
|
||||
),
|
||||
Arrays.asList(
|
||||
Arrays.asList("g", "h", "i"),
|
||||
Arrays.asList("j", "k", "l")
|
||||
)
|
||||
);
|
||||
List<Object> flat = CollUtil.flat(list, false);
|
||||
Assertions.assertArrayEquals(new String[]{"a", "b", "c", null, "d", "e", "f", "g", "h", "i", "j", "k", "l"}, flat.toArray());
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ 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;
|
||||
@ -270,22 +269,4 @@ 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