From fc532551af8f8a338348c6a66779f9f7e897da7a Mon Sep 17 00:00:00 2001 From: VampireAchao Date: Fri, 9 Sep 2022 14:41:10 +0800 Subject: [PATCH] =?UTF-8?q?:trollface:=20=E5=A2=9E=E5=BC=BAEasyStream#zip?= =?UTF-8?q?=E5=AF=B9=E5=B9=B6=E8=A1=8C=E6=B5=81=E7=9A=84=E6=94=AF=E6=8C=81?= =?UTF-8?q?=EF=BC=8C=E6=8F=90=E4=BE=9BCollectorUtil.entryToMap=E5=AF=B9Ent?= =?UTF-8?q?ry=E8=BD=ACmap=E6=8F=90=E4=BE=9B=E6=94=AF=E6=8C=81=EF=BC=8C?= =?UTF-8?q?=E5=AE=8C=E5=96=84javadoc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../stream/TransformableWrappedStream.java | 44 +++++++------------ 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/stream/TransformableWrappedStream.java b/hutool-core/src/main/java/cn/hutool/core/stream/TransformableWrappedStream.java index d621278da..ad7b314cc 100644 --- a/hutool-core/src/main/java/cn/hutool/core/stream/TransformableWrappedStream.java +++ b/hutool-core/src/main/java/cn/hutool/core/stream/TransformableWrappedStream.java @@ -3,7 +3,6 @@ package cn.hutool.core.stream; import cn.hutool.core.collection.ListUtil; import cn.hutool.core.collection.iter.IterUtil; 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.MutableObj; import cn.hutool.core.map.MapUtil; @@ -40,24 +39,15 @@ public interface TransformableWrappedStream EasyStream zip( - final Iterable other, - final BiFunction zipper) { + final Iterable other, + final BiFunction zipper) { Objects.requireNonNull(zipper); - final Spliterator keys = spliterator(); - final Spliterator values = Opt.ofNullable(other).map(Iterable::spliterator).orElseGet(Spliterators::emptySpliterator); - // 获取两个Spliterator的中较小的数量 - // 如果Spliterator经过流操作, getExactSizeIfKnown()可能会返回-1, 所以默认大小为 ArrayList.DEFAULT_CAPACITY - final int sizeIfKnown = (int) Math.max(Math.min(keys.getExactSizeIfKnown(), values.getExactSizeIfKnown()), 10); - final List list = new ArrayList<>(sizeIfKnown); - // 保存第一个Spliterator的值 - final MutableObj key = new MutableObj<>(); - // 保存第二个Spliterator的值 - final MutableObj value = new MutableObj<>(); - // 当两个Spliterator中都还有剩余元素时 - while (keys.tryAdvance(key::set) && values.tryAdvance(value::set)) { - list.add(zipper.apply(key.get(), value.get())); + Map idxIdentityMap = mapIdx((e, idx) -> MapUtil.entry(idx, e)).collect(CollectorUtil.entryToMap()); + Map idxOtherMap = EasyStream.of(other).mapIdx((e, idx) -> MapUtil.entry(idx, e)).collect(CollectorUtil.entryToMap()); + if (idxIdentityMap.size() <= idxOtherMap.size()) { + return EasyStream.of(idxIdentityMap.keySet(), isParallel()).map(k -> zipper.apply(idxIdentityMap.get(k), idxOtherMap.get(k))); } - 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))); } /** @@ -78,9 +68,9 @@ public interface TransformableWrappedStream>of(EasyStream.of(list, isParallel())); } return EasyStream.iterate(0, i -> i < size, i -> i + batchSize) - .map(skip -> EasyStream.of(list.subList(skip, Math.min(size, skip + batchSize)), isParallel())) - .parallel(isParallel()) - .onClose(unwrap()::close); + .map(skip -> EasyStream.of(list.subList(skip, Math.min(size, skip + batchSize)), isParallel())) + .parallel(isParallel()) + .onClose(unwrap()::close); } /** @@ -114,8 +104,8 @@ public interface TransformableWrappedStream 键类型 + * @param keyMapper 键的映射方法 + * @param 键类型 * @return {@link EntryStream}实例 */ default EntryStream toEntries(final Function keyMapper) { @@ -159,7 +149,7 @@ public interface TransformableWrappedStream elements = unwrap().collect(Collectors.toList()); return wrap(ListUtil.splice(elements, start, deleteCount, items).stream()) - .parallel(isParallel()); + .parallel(isParallel()); } /** @@ -262,6 +252,7 @@ public interface TransformableWrappedStream>> recursiveRef = new MutableObj<>(); - @SuppressWarnings("unchecked") - final Function> recursive = e -> EasyStream.of(childrenGetter.apply(e)) - .flat(recursiveRef.get()) - .unshift(e); + @SuppressWarnings("unchecked") final Function> recursive = e -> EasyStream.of(childrenGetter.apply(e)) + .flat(recursiveRef.get()) + .unshift(e); recursiveRef.set(recursive); return wrap(flatMap(recursive).peek(e -> childrenSetter.accept(e, null))); }