添加flat方法

This commit is contained in:
kongweiguang 2023-07-03 09:55:23 +08:00
parent 13fe08c73f
commit 844b05cc35
3 changed files with 16 additions and 12 deletions

View File

@ -2363,13 +2363,14 @@ public class CollUtil {
* @param collection 需要解构的集合 * @param collection 需要解构的集合
* @return 解构后的集合 * @return 解构后的集合
*/ */
public static List<Object> flat(Collection<?> collection) { public static <T> List<T> flat(Collection<?> collection) {
return flat(collection, true); return flat(collection, true);
} }
/** /**
* 解构多层集合 * 解构多层集合
* 例如List&lt;List&lt;List&lt;String&gt;&gt;&gt; 解构成 List&lt;String&gt; * 例如例如{@code List<List<List<String>>> 解构成 List<String>}
* <p>
* skipNull如果为true, 则解构后的集合里不包含null值为false则会包含null值 * skipNull如果为true, 则解构后的集合里不包含null值为false则会包含null值
* *
* @param collection 需要结构的集合 * @param collection 需要结构的集合
@ -2377,7 +2378,7 @@ public class CollUtil {
* @return 解构后的集合 * @return 解构后的集合
*/ */
@SuppressWarnings({"unchecked"}) @SuppressWarnings({"unchecked"})
public static List<Object> flat(Collection<?> collection, boolean skipNull) { public static <T> List<T> flat(Collection<?> collection, boolean skipNull) {
LinkedList<Object> queue = new LinkedList<>(collection); LinkedList<Object> queue = new LinkedList<>(collection);
List<Object> result = new ArrayList<>(); List<Object> result = new ArrayList<>();
@ -2385,14 +2386,16 @@ public class CollUtil {
while (!queue.isEmpty()) { while (!queue.isEmpty()) {
Object t = queue.removeFirst(); Object t = queue.removeFirst();
if (skipNull && t == null) {
continue;
}
if (t instanceof Collection) { if (t instanceof Collection) {
queue.addAll((Collection<?>) t); queue.addAll((Collection<?>) t);
} else if (skipNull && t == null) {
continue;
} else { } else {
result.add(t); result.add(t);
} }
} }
return result; return (List<T>) result;
} }
} }

View File

@ -489,14 +489,15 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
/** /**
* 如果当前元素是集合则会将集合中的元素解构出来 * 如果当前元素是集合则会将集合中的元素解构出来
* 例如List&lt;List&lt;List&lt;String&gt;&gt;&gt; 解构成 List&lt;String&gt; * 例如{@code List<List<List<String>>> 解构成 List<String>}
* @param clazz 解构后元素的类型 *
* @param <R> 函数执行后返回的类型 * @param <R> 函数执行后返回的List里面的类型
* @return EasyStream 一个流 * @return EasyStream 一个流
*/ */
default <R> EasyStream<R> flatObj(Class<R> clazz) { default <R> EasyStream<R> flat() {
return EasyStream.of(CollUtil.flat(nonNull().collect(Collectors.toList()))).map(clazz::cast); return EasyStream.of(CollUtil.flat(nonNull().collect(Collectors.toList())));
} }
// endregion // endregion
// region ============ map ============ // region ============ map ============

View File

@ -723,7 +723,7 @@ public class AbstractEnhancedWrappedStreamTest {
Arrays.asList("j", "k", "l") Arrays.asList("j", "k", "l")
) )
); );
List<String> r = EasyStream.of(list).flatObj(String.class).toList(); List<String> r = EasyStream.of(list).<String>flat().toList();
Assertions.assertArrayEquals(new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"}, r.toArray()); Assertions.assertArrayEquals(new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"}, r.toArray());
} }