From aae72484f19d62c98cfb8490ccd126d7be03ce20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=87=A7=E8=87=A7?= <2556450572@qq.com> Date: Wed, 24 Aug 2022 13:16:31 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=AF=B9EasyStream=E4=B8=AD=E8=A1=A5?= =?UTF-8?q?=E5=85=85peekIdx()=E6=96=B9=E6=B3=95,mapMulti=E9=80=82=E9=85=8D?= =?UTF-8?q?=E4=BA=8E=E6=9B=B4=E9=AB=98=E7=89=88=E6=9C=ACJDK?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/stream/EasyStream.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/stream/EasyStream.java b/hutool-core/src/main/java/cn/hutool/core/stream/EasyStream.java index dbc35d1e0..cd8f71d19 100644 --- a/hutool-core/src/main/java/cn/hutool/core/stream/EasyStream.java +++ b/hutool-core/src/main/java/cn/hutool/core/stream/EasyStream.java @@ -25,6 +25,7 @@ import java.util.Spliterator; import java.util.Spliterators; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.BiPredicate; @@ -526,7 +527,7 @@ public class EasyStream implements Stream, Iterable { * @param 拆分后流的元素类型 * @return 返回叠加拆分操作后的流 */ - public EasyStream mapMulti(final BiConsumer> mapper) { + public EasyStream mapMulti(final BiConsumer> mapper) { Objects.requireNonNull(mapper); return flatMap(e -> { final FastStreamBuilder buffer = EasyStream.builder(); @@ -630,6 +631,31 @@ public class EasyStream implements Stream, Iterable { return new EasyStream<>(stream.peek(action)); } + /** + * 返回与指定函数将元素作为参数执行后组成的流。操作带下标,并行流时下标永远为-1 + * 这是一个无状态中间操作 + * @param action 指定的函数 + * @return 返回叠加操作后的FastStream + * @apiNote 该方法存在的意义主要是用来调试 + * 当你需要查看经过操作管道某处的元素和下标,可以执行以下操作: + *
{@code
+	 *     .of("one", "two", "three", "four")
+	 * 				.filter(e -> e.length() > 3)
+	 * 				.peekIdx((e,i) -> System.out.println("Filtered value: " + e + " Filtered idx:" + i))
+	 * 				.map(String::toUpperCase)
+	 * 				.peekIdx((e,i) -> System.out.println("Mapped value: " + e + " Filtered idx:" + i))
+	 * 				.collect(Collectors.toList());
+	 * }
+ */ + public EasyStream peekIdx(BiConsumer action) { + Objects.requireNonNull(action); + if (isParallel()) { + return peek(e -> action.accept(e, NOT_FOUND_INDEX)); + } else { + AtomicInteger index = new AtomicInteger(NOT_FOUND_INDEX); + return peek(e -> action.accept(e, index.incrementAndGet())); + } + } /** * 返回叠加调用{@link Console#log(Object)}打印出结果的流 * From e410b2685e764935fa38a5c29aaf6f625b5cf397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=87=A7=E8=87=A7?= <2556450572@qq.com> Date: Wed, 24 Aug 2022 13:26:37 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=AF=B9EasyStream=E4=B8=AD=E8=A1=A5?= =?UTF-8?q?=E5=85=85peekIdx()=E6=96=B9=E6=B3=95,mapMulti=E9=80=82=E9=85=8D?= =?UTF-8?q?=E4=BA=8E=E6=9B=B4=E9=AB=98=E7=89=88=E6=9C=ACJDK?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hutool-core/src/main/java/cn/hutool/core/stream/EasyStream.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/stream/EasyStream.java b/hutool-core/src/main/java/cn/hutool/core/stream/EasyStream.java index cd8f71d19..daa942149 100644 --- a/hutool-core/src/main/java/cn/hutool/core/stream/EasyStream.java +++ b/hutool-core/src/main/java/cn/hutool/core/stream/EasyStream.java @@ -643,7 +643,7 @@ public class EasyStream implements Stream, Iterable { * .filter(e -> e.length() > 3) * .peekIdx((e,i) -> System.out.println("Filtered value: " + e + " Filtered idx:" + i)) * .map(String::toUpperCase) - * .peekIdx((e,i) -> System.out.println("Mapped value: " + e + " Filtered idx:" + i)) + * .peekIdx((e,i) -> System.out.println("Mapped value: " + e + " Mapped idx:" + i)) * .collect(Collectors.toList()); * } */ From 97ce543b4726dc6469500fe9e83d3522c954fe72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=87=A7=E8=87=A7?= <2556450572@qq.com> Date: Wed, 24 Aug 2022 13:49:09 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=AF=B9EasyStream=E4=B8=AD=E8=A1=A5?= =?UTF-8?q?=E5=85=85peekIdx()=E6=96=B9=E6=B3=95,mapMulti=E9=80=82=E9=85=8D?= =?UTF-8?q?=E4=BA=8E=E6=9B=B4=E9=AB=98=E7=89=88=E6=9C=ACJDK?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/stream/EasyStreamTest.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/hutool-core/src/test/java/cn/hutool/core/stream/EasyStreamTest.java b/hutool-core/src/test/java/cn/hutool/core/stream/EasyStreamTest.java index 098fc12b7..6312d17e3 100644 --- a/hutool-core/src/test/java/cn/hutool/core/stream/EasyStreamTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/stream/EasyStreamTest.java @@ -208,6 +208,26 @@ public class EasyStreamTest { Assert.assertEquals(Arrays.asList(-1, -1, -1), EasyStream.of(1, 2, 3).parallel().flatMapIdx((e, i) -> EasyStream.of(i)).toList()); } + @Test + public void testPeek(){ + EasyStream.of("one", "two", "three", "four") + .filter(e -> e.length() == 4) + .peek(e -> Assert.assertEquals("four", e)) + .map(String::toUpperCase) + .peek(e -> Assert.assertEquals("FOUR", e)) + .collect(Collectors.toList()); + } + + @Test + public void testPeekIdx(){ + EasyStream.of("one", "two", "three", "four") + .filter(e -> e.length() == 4) + .peekIdx((e,i) -> Assert.assertEquals("four:0", e + ":" + i)) + .map(String::toUpperCase) + .peekIdx((e,i) -> Assert.assertEquals("FOUR:0", e + ":" + i)) + .collect(Collectors.toList()); + } + @Test public void testFlat() { final List list = Arrays.asList(1, 2, 3);