mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
添加flatObj方法
This commit is contained in:
parent
4e047e98e3
commit
9bc9dca805
@ -2357,7 +2357,7 @@ public class CollUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 结构多层集合
|
||||
* 解构多层集合
|
||||
* 例如:List<List<List<String>>> 解构成 List<String>
|
||||
*
|
||||
* @param collection 需要解构的集合
|
||||
@ -2371,7 +2371,7 @@ public class CollUtil {
|
||||
* 结构多层集合
|
||||
* 例如:List<List<List<String>>> 解构成 List<String>
|
||||
* <p>
|
||||
* skipNull的作用是当集合里面有个值为空,当为true是解构后的集合里面没有null值,如果为false则会在解构后的集合里面有努力了。
|
||||
* skipNull如果为true, 则解构后的集合里不包含null值,为false则会包含null值。
|
||||
*
|
||||
* @param collection 需要结构的集合
|
||||
* @param skipNull 是否跳过空的值
|
||||
|
@ -13,6 +13,7 @@
|
||||
package org.dromara.hutool.core.stream;
|
||||
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.dromara.hutool.core.collection.CollUtil;
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import org.dromara.hutool.core.collection.iter.IterUtil;
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
@ -486,6 +487,16 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
||||
return wrap(flatMap(recursive).peek(e -> childrenSetter.accept(e, null)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果当前元素是集合,则会解构当前集合
|
||||
*
|
||||
* @param clazz 解构后元素的类型
|
||||
* @param <R> 函数执行后返回的类型
|
||||
* @return EasyStream 一个流
|
||||
*/
|
||||
default <R> EasyStream<R> flatObj(Class<R> clazz) {
|
||||
return EasyStream.of(CollUtil.flat(nonNull().collect(Collectors.toList()))).map(clazz::cast);
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region ============ map ============
|
||||
|
@ -1,40 +1,19 @@
|
||||
package org.dromara.hutool.core.collection;
|
||||
|
||||
import lombok.*;
|
||||
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;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.core.stream.EasyStream;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
import java.util.Stack;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
@ -1229,4 +1208,5 @@ public class CollUtilTest {
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -708,4 +708,23 @@ public class AbstractEnhancedWrappedStreamTest {
|
||||
private List<Tree> children;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
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<String> r = EasyStream.of(list).flatObj(String.class).toList();
|
||||
Assertions.assertArrayEquals(new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"}, r.toArray());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user