!802 【6.x】增强EasyStream#zip对并行流的支持,提供CollectorUtil.entryToMap对Entry转map提供支持

Merge pull request !802 from 阿超/v6-dev
This commit is contained in:
Looly 2022-09-10 14:29:39 +00:00 committed by Gitee
commit c72bc8c721
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 96 additions and 96 deletions

View File

@ -8,7 +8,7 @@ import java.util.stream.Stream;
* *
* @param <T> 流中的元素类型 * @param <T> 流中的元素类型
* @param <S> {@link AbstractEnhancedWrappedStream}的实现类类型 * @param <S> {@link AbstractEnhancedWrappedStream}的实现类类型
* @author huangchengxing * @author huangchengxing VampireAchao
* @see EasyStream * @see EasyStream
* @see EntryStream * @see EntryStream
* @since 6.0.0 * @since 6.0.0

View File

@ -361,4 +361,14 @@ public class CollectorUtil {
return transform(ArrayList::new, mapper); return transform(ArrayList::new, mapper);
} }
/**
* 用于{@code Stream<Entry>} Map 的情况
*
* @param <K> key类型
* @param <V> value类型
* @return map
*/
public static <K, V> Collector<Map.Entry<K, V>, ?, Map<K, V>> entryToMap() {
return toMap(Map.Entry::getKey, Map.Entry::getValue);
}
} }

View File

@ -17,7 +17,7 @@ import java.util.stream.Stream;
* *
* @param <T> 流中的元素类型 * @param <T> 流中的元素类型
* @param <S> {@link TerminableWrappedStream}的实现类类型 * @param <S> {@link TerminableWrappedStream}的实现类类型
* @author huangchengxing * @author huangchengxing VampireAchao
* @since 6.0.0 * @since 6.0.0
*/ */
public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T, S>> extends WrappedStream<T, S> { public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T, S>> extends WrappedStream<T, S> {

View File

@ -3,7 +3,6 @@ package cn.hutool.core.stream;
import cn.hutool.core.collection.ListUtil; import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.collection.iter.IterUtil; import cn.hutool.core.collection.iter.IterUtil;
import cn.hutool.core.lang.Console; import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.lang.mutable.MutableInt; import cn.hutool.core.lang.mutable.MutableInt;
import cn.hutool.core.lang.mutable.MutableObj; import cn.hutool.core.lang.mutable.MutableObj;
import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.MapUtil;
@ -24,7 +23,7 @@ import java.util.stream.StreamSupport;
* *
* @param <T> 流中的元素类型 * @param <T> 流中的元素类型
* @param <S> {@link TransformableWrappedStream}的实现类类型 * @param <S> {@link TransformableWrappedStream}的实现类类型
* @author huangchengxing * @author huangchengxing VampireAchao
* @since 6.0.0 * @since 6.0.0
*/ */
public interface TransformableWrappedStream<T, S extends TransformableWrappedStream<T, S>> extends WrappedStream<T, S> { public interface TransformableWrappedStream<T, S extends TransformableWrappedStream<T, S>> extends WrappedStream<T, S> {
@ -43,21 +42,12 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
final Iterable<U> other, final Iterable<U> other,
final BiFunction<? super T, ? super U, ? extends R> zipper) { final BiFunction<? super T, ? super U, ? extends R> zipper) {
Objects.requireNonNull(zipper); Objects.requireNonNull(zipper);
final Spliterator<T> keys = spliterator(); Map<Integer, T> idxIdentityMap = mapIdx((e, idx) -> MapUtil.entry(idx, e)).collect(CollectorUtil.entryToMap());
final Spliterator<U> values = Opt.ofNullable(other).map(Iterable::spliterator).orElseGet(Spliterators::emptySpliterator); Map<Integer, U> idxOtherMap = EasyStream.of(other).mapIdx((e, idx) -> MapUtil.entry(idx, e)).collect(CollectorUtil.entryToMap());
// 获取两个Spliterator的中较小的数量 if (idxIdentityMap.size() <= idxOtherMap.size()) {
// 如果Spliterator经过流操作, getExactSizeIfKnown()可能会返回-1, 所以默认大小为 ArrayList.DEFAULT_CAPACITY return EasyStream.of(idxIdentityMap.keySet(), isParallel()).map(k -> zipper.apply(idxIdentityMap.get(k), idxOtherMap.get(k)));
final int sizeIfKnown = (int) Math.max(Math.min(keys.getExactSizeIfKnown(), values.getExactSizeIfKnown()), 10);
final List<R> list = new ArrayList<>(sizeIfKnown);
// 保存第一个Spliterator的值
final MutableObj<T> key = new MutableObj<>();
// 保存第二个Spliterator的值
final MutableObj<U> value = new MutableObj<>();
// 当两个Spliterator中都还有剩余元素时
while (keys.tryAdvance(key::set) && values.tryAdvance(value::set)) {
list.add(zipper.apply(key.get(), value.get()));
} }
return EasyStream.of(list).parallel(isParallel()).onClose(unwrap()::close); return EasyStream.of(idxOtherMap.keySet(), isParallel()).map(k -> zipper.apply(idxIdentityMap.get(k), idxOtherMap.get(k)));
} }
/** /**
@ -262,6 +252,7 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
/** /**
* 返回与指定函数将元素作为参数执行后组成的流操作带下标并行流时下标永远为-1 * 返回与指定函数将元素作为参数执行后组成的流操作带下标并行流时下标永远为-1
* 这是一个无状态中间操作 * 这是一个无状态中间操作
*
* @param action 指定的函数 * @param action 指定的函数
* @return 返回叠加操作后的FastStream * @return 返回叠加操作后的FastStream
* @apiNote 该方法存在的意义主要是用来调试 * @apiNote 该方法存在的意义主要是用来调试
@ -491,8 +482,7 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
Objects.requireNonNull(childrenGetter); Objects.requireNonNull(childrenGetter);
Objects.requireNonNull(childrenSetter); Objects.requireNonNull(childrenSetter);
final MutableObj<Function<T, EasyStream<T>>> recursiveRef = new MutableObj<>(); final MutableObj<Function<T, EasyStream<T>>> recursiveRef = new MutableObj<>();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked") final Function<T, EasyStream<T>> recursive = e -> EasyStream.of(childrenGetter.apply(e))
final Function<T, EasyStream<T>> recursive = e -> EasyStream.of(childrenGetter.apply(e))
.flat(recursiveRef.get()) .flat(recursiveRef.get())
.unshift(e); .unshift(e);
recursiveRef.set(recursive); recursiveRef.set(recursive);

View File

@ -636,7 +636,7 @@ public class AbstractEnhancedWrappedStreamTest {
List<String> zip = wrap(orders).zip(list, (e1, e2) -> e1 + "." + e2).toList(); List<String> zip = wrap(orders).zip(list, (e1, e2) -> e1 + "." + e2).toList();
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), zip); Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), zip);
zip = wrap((Stream<? extends Object>)EasyStream.iterate(1, i -> i + 1)).zip(list, (e1, e2) -> e1 + "." + e2).toList(); zip = wrap((Stream<? extends Object>) EasyStream.iterate(1, i -> i + 1)).limit(10).zip(list, (e1, e2) -> e1 + "." + e2).toList();
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), zip); Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), zip);
} }