From 1af96de3ae2148135b7e8930018e1e112cb7c72e Mon Sep 17 00:00:00 2001 From: Looly Date: Mon, 19 Sep 2022 09:52:40 +0800 Subject: [PATCH] =?UTF-8?q?StreamUtil.of=E6=96=B9=E6=B3=95=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E5=AF=B9=20Iterator=20=E6=94=AF=E6=8C=81=EF=BC=9BStre?= =?UTF-8?q?amUtil.of(Iterable)=20=E6=96=B9=E6=B3=95=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 ++- .../java/cn/hutool/core/stream/StreamUtil.java | 2 +- .../cn/hutool/core/stream/StreamUtilTest.java | 15 +++++++++++---- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e59169d48..2f3c89e4c 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,9 +3,10 @@ ------------------------------------------------------------------------------------------------------------- -# 5.8.8.M1 (2022-09-17) +# 5.8.8.M1 (2022-09-19) ### 🐣新特性 +* 【core 】 StreamUtil.of方法新增对 Iterator 支持;StreamUtil.of(Iterable) 方法优化(pr#807@Gitee) ### 🐞Bug修复 * 【core 】 修复FileNameUtil.cleanInvalid无法去除换行符问题(issue#I5RMZV@Gitee) diff --git a/hutool-core/src/main/java/cn/hutool/core/stream/StreamUtil.java b/hutool-core/src/main/java/cn/hutool/core/stream/StreamUtil.java index 6b654dd73..475a54201 100644 --- a/hutool-core/src/main/java/cn/hutool/core/stream/StreamUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/stream/StreamUtil.java @@ -54,7 +54,7 @@ public class StreamUtil { Assert.notNull(iterable, "Iterable must be not null!"); return iterable instanceof Collection ? - ((Collection) iterable).stream() : + parallel ? ((Collection) iterable).parallelStream() : ((Collection) iterable).stream() : StreamSupport.stream(iterable.spliterator(), parallel); } diff --git a/hutool-core/src/test/java/cn/hutool/core/stream/StreamUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/stream/StreamUtilTest.java index edccf5b3c..cb4d48dfe 100644 --- a/hutool-core/src/test/java/cn/hutool/core/stream/StreamUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/stream/StreamUtilTest.java @@ -5,6 +5,7 @@ import org.junit.Assert; import org.junit.Test; import java.util.ArrayList; +import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.stream.Collectors; @@ -25,21 +26,27 @@ public class StreamUtilTest { Assert.assertThrows(IllegalArgumentException.class, () -> StreamUtil.of((Iterator) null)); } + @SuppressWarnings({"RedundantOperationOnEmptyContainer", "RedundantCollectionOperation"}) @Test - public void streamTestEmptyIterator() { + public void streamTestEmptyListToIterator() { assertStreamIsEmpty(StreamUtil.of(new ArrayList<>().iterator())); } + @Test + public void streamTestEmptyIterator() { + assertStreamIsEmpty(StreamUtil.of(Collections.emptyIterator())); + } + @Test public void streamTestOrdinaryIterator() { - ArrayList arrayList = CollUtil.newArrayList(1, 2, 3); + final ArrayList arrayList = CollUtil.newArrayList(1, 2, 3); Assert.assertArrayEquals(new Integer[]{1, 2, 3}, StreamUtil.of(arrayList.iterator()).toArray()); - HashSet hashSet = CollUtil.newHashSet(1, 2, 3); + final HashSet hashSet = CollUtil.newHashSet(1, 2, 3); Assert.assertEquals(hashSet, StreamUtil.of(hashSet.iterator()).collect(Collectors.toSet())); } - void assertStreamIsEmpty(Stream stream) { + void assertStreamIsEmpty(final Stream stream) { Assert.assertNotNull(stream); Assert.assertEquals(0, stream.toArray().length); }