mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add flat
This commit is contained in:
parent
24361faace
commit
ba32ec4305
@ -2359,10 +2359,11 @@ public class CollUtil {
|
||||
* 解构多层集合
|
||||
* 例如:{@code List<List<List<String>>> 解构成 List<String>}
|
||||
*
|
||||
* @param <T> 元素类型
|
||||
* @param collection 需要解构的集合
|
||||
* @return 解构后的集合
|
||||
*/
|
||||
public static <T> List<T> flat(Collection<?> collection) {
|
||||
public static <T> List<T> flat(final Collection<?> collection) {
|
||||
return flat(collection, true);
|
||||
}
|
||||
|
||||
@ -2372,18 +2373,19 @@ public class CollUtil {
|
||||
* <p>
|
||||
* skipNull如果为true, 则解构后的集合里不包含null值,为false则会包含null值。
|
||||
*
|
||||
* @param <T> 元素类型
|
||||
* @param collection 需要结构的集合
|
||||
* @param skipNull 是否跳过空的值
|
||||
* @return 解构后的集合
|
||||
*/
|
||||
@SuppressWarnings({"unchecked"})
|
||||
public static <T> List<T> flat(Collection<?> collection, boolean skipNull) {
|
||||
LinkedList<Object> queue = new LinkedList<>(collection);
|
||||
public static <T> List<T> flat(final Collection<?> collection, final boolean skipNull) {
|
||||
final LinkedList<Object> queue = new LinkedList<>(collection);
|
||||
|
||||
List<Object> result = new ArrayList<>();
|
||||
final List<Object> result = new ArrayList<>();
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
Object t = queue.removeFirst();
|
||||
while (isNotEmpty(queue)) {
|
||||
final Object t = queue.removeFirst();
|
||||
|
||||
if (skipNull && t == null) {
|
||||
continue;
|
||||
|
@ -493,6 +493,7 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
||||
*
|
||||
* @param <R> 函数执行后返回的List里面的类型
|
||||
* @return EasyStream 一个流
|
||||
* @since 6.0.0
|
||||
*/
|
||||
default <R> EasyStream<R> flat() {
|
||||
return EasyStream.of(CollUtil.flat(nonNull().collect(Collectors.toList())));
|
||||
|
@ -8,7 +8,6 @@ import org.dromara.hutool.core.date.DateUtil;
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.core.map.Dict;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.core.stream.EasyStream;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -1206,19 +1205,21 @@ public class CollUtilTest {
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("ArraysAsListWithZeroOrOneArgument")
|
||||
@Test
|
||||
public void flatListTest1() {
|
||||
List<List<List<String>>> list = Arrays.asList(Arrays.asList(Arrays.asList("1", "2", "3"), Arrays.asList("5", "6", "7")));
|
||||
final 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);
|
||||
final List<Object> objects = CollUtil.flat(list);
|
||||
|
||||
Assertions.assertArrayEquals(new String[]{"1", "2", "3", "5", "6", "7"}, objects.toArray());
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("ArraysAsListWithZeroOrOneArgument")
|
||||
@Test
|
||||
public void flatListTest2() {
|
||||
List<List<List<String>>> list = Arrays.asList(
|
||||
final List<List<List<String>>> list = Arrays.asList(
|
||||
Arrays.asList(
|
||||
Arrays.asList("a"),
|
||||
Arrays.asList("b", "c"),
|
||||
@ -1229,15 +1230,16 @@ public class CollUtilTest {
|
||||
Arrays.asList("j", "k", "l")
|
||||
)
|
||||
);
|
||||
List<Object> flat = CollUtil.flat(list);
|
||||
final List<Object> flat = CollUtil.flat(list);
|
||||
Assertions.assertArrayEquals(new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"}, flat.toArray());
|
||||
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("ArraysAsListWithZeroOrOneArgument")
|
||||
@Test
|
||||
void flatListTest3() {
|
||||
List<List<List<String>>> list = Arrays.asList(
|
||||
final List<List<List<String>>> list = Arrays.asList(
|
||||
Arrays.asList(
|
||||
Arrays.asList("a"),
|
||||
Arrays.asList("b", "c", null),
|
||||
@ -1248,7 +1250,7 @@ public class CollUtilTest {
|
||||
Arrays.asList("j", "k", "l")
|
||||
)
|
||||
);
|
||||
List<Object> flat = CollUtil.flat(list, false);
|
||||
final 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());
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user