From 143b8013ad8afdc19f8e79e3338932928fe4e558 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=98=8A=E5=9F=95?= <1678591193@qq.com> Date: Tue, 9 Apr 2024 19:15:56 +0800 Subject: [PATCH 1/3] subList --- .../stream/core/stream/collector/Collective.java | 14 ++++++++++++++ .../core/stream/collector/CollectiveTest.java | 15 +++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/stream-core/src/main/java/org/dromara/streamquery/stream/core/stream/collector/Collective.java b/stream-core/src/main/java/org/dromara/streamquery/stream/core/stream/collector/Collective.java index 4b414dac..cb8376dd 100644 --- a/stream-core/src/main/java/org/dromara/streamquery/stream/core/stream/collector/Collective.java +++ b/stream-core/src/main/java/org/dromara/streamquery/stream/core/stream/collector/Collective.java @@ -116,6 +116,20 @@ public class Collective { CH_ID); } + public static Collector> listSkipping(int skip) { + return subList(skip, null); + } + + public static Collector> subList(Integer skip, Integer limit) { + Integer offset = Opp.of(skip).orElse(0); + return mapping(Function.identity(), collectingAndThen(toList(), list -> { + int size = list.size(); + int fromIndex = offset > size ? size : offset; + int toIndex = Math.min(fromIndex + limit, size); + return list.subList(fromIndex, toIndex); + })); + } + /** * Returns a {@code Collector} that accumulates the input elements into a new {@code Set}. There * are no guarantees on the type, mutability, serializability, or thread-safety of the {@code Set} diff --git a/stream-core/src/test/java/org/dromara/streamquery/stream/core/stream/collector/CollectiveTest.java b/stream-core/src/test/java/org/dromara/streamquery/stream/core/stream/collector/CollectiveTest.java index 307b00eb..4dff1553 100644 --- a/stream-core/src/test/java/org/dromara/streamquery/stream/core/stream/collector/CollectiveTest.java +++ b/stream-core/src/test/java/org/dromara/streamquery/stream/core/stream/collector/CollectiveTest.java @@ -22,6 +22,7 @@ import org.junit.jupiter.api.Test; import java.util.*; import java.util.function.Function; +import java.util.stream.Collector; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -62,6 +63,20 @@ class CollectiveTest { .isEmpty()); } + @Test + void testGroupingBySkip() { + List list = Arrays.asList(0, 0, 0, 1, 1, 1, 2, 2, 2); + Collector>> grouping = groupingBy(i -> i * 2, + Collectors.mapping(i -> i + 1, + Collectors.collectingAndThen(toList(), a -> a.subList(0, 2)))); + Collector>> grouping1 = groupingBy(i -> i * 2, subList(0, 2)); + + Map> collect1 = list.stream().collect(grouping); + Map> collect = list.stream().collect(grouping1); + System.out.println(collect1); + System.out.println(collect); + } + @Test void testFlatMapping() { List actual = -- Gitee From 2cde74aae19e6f4fa1039108d9f1171ae5845275 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=98=8A=E5=9F=95?= <1678591193@qq.com> Date: Fri, 12 Apr 2024 17:26:43 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=80=E4=B8=AAsubList?= =?UTF-8?q?()=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/stream/collector/Collective.java | 16 ++++++++-- .../core/stream/collector/CollectiveTest.java | 29 ++++++++++++------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/stream-core/src/main/java/org/dromara/streamquery/stream/core/stream/collector/Collective.java b/stream-core/src/main/java/org/dromara/streamquery/stream/core/stream/collector/Collective.java index cb8376dd..1fbf2e85 100644 --- a/stream-core/src/main/java/org/dromara/streamquery/stream/core/stream/collector/Collective.java +++ b/stream-core/src/main/java/org/dromara/streamquery/stream/core/stream/collector/Collective.java @@ -116,16 +116,28 @@ public class Collective { CH_ID); } - public static Collector> listSkipping(int skip) { + public static Collector> limiting(int limit) { + return subList(null, limit); + } + + public static Collector> skipping(int skip) { return subList(skip, null); } + /** + * + * @param skip + * @param limit + * @return a {@code Collector} which collects all the input elements into a {@code Set} + * @param the type of the input elements + */ public static Collector> subList(Integer skip, Integer limit) { Integer offset = Opp.of(skip).orElse(0); + Integer limited = Opp.of(limit).orElse(0); return mapping(Function.identity(), collectingAndThen(toList(), list -> { int size = list.size(); int fromIndex = offset > size ? size : offset; - int toIndex = Math.min(fromIndex + limit, size); + int toIndex = Math.min(fromIndex + limited, size); return list.subList(fromIndex, toIndex); })); } diff --git a/stream-core/src/test/java/org/dromara/streamquery/stream/core/stream/collector/CollectiveTest.java b/stream-core/src/test/java/org/dromara/streamquery/stream/core/stream/collector/CollectiveTest.java index 4dff1553..10231e45 100644 --- a/stream-core/src/test/java/org/dromara/streamquery/stream/core/stream/collector/CollectiveTest.java +++ b/stream-core/src/test/java/org/dromara/streamquery/stream/core/stream/collector/CollectiveTest.java @@ -22,7 +22,6 @@ import org.junit.jupiter.api.Test; import java.util.*; import java.util.function.Function; -import java.util.stream.Collector; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -64,17 +63,25 @@ class CollectiveTest { } @Test - void testGroupingBySkip() { + void testSubList() { List list = Arrays.asList(0, 0, 0, 1, 1, 1, 2, 2, 2); - Collector>> grouping = groupingBy(i -> i * 2, - Collectors.mapping(i -> i + 1, - Collectors.collectingAndThen(toList(), a -> a.subList(0, 2)))); - Collector>> grouping1 = groupingBy(i -> i * 2, subList(0, 2)); - - Map> collect1 = list.stream().collect(grouping); - Map> collect = list.stream().collect(grouping1); - System.out.println(collect1); - System.out.println(collect); + Map> map1 = list.stream().collect(groupingBy(Function.identity(), subList(1, 2))); + Assertions.assertEquals(3, map1.size()); + Assertions.assertEquals(2, map1.get(0).size()); + Assertions.assertEquals(2, map1.get(1).size()); + Assertions.assertEquals(2, map1.get(2).size()); + + Map> map2 = list.stream().collect(groupingBy(Function.identity(), subList(10, 2))); + Assertions.assertEquals(3, map2.size()); + Assertions.assertEquals(0, map2.get(0).size()); + Assertions.assertEquals(0, map2.get(1).size()); + Assertions.assertEquals(0, map2.get(2).size()); + + + + Assertions.assertThrows(, () -> { + list.stream().collect(groupingBy(Function.identity(), subList(1, 0))); + }); } @Test -- Gitee From 0ea8cc4db53c034f1d8f9ec65df30d76b45abc63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=98=8A=E5=9F=95?= <1678591193@qq.com> Date: Mon, 15 Apr 2024 11:46:10 +0800 Subject: [PATCH 3/3] =?UTF-8?q?groupby=E5=88=86=E7=BB=84=E5=90=8E=EF=BC=8C?= =?UTF-8?q?=E5=AF=B9=E6=AF=8F=E4=B8=80=E4=B8=AA=E5=88=86=E7=BB=84=E9=A1=B9?= =?UTF-8?q?=E8=BF=9B=E8=A1=8Cskip=EF=BC=8Climit=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/stream/collector/Collective.java | 28 +++++++--- .../core/stream/collector/CollectiveTest.java | 54 ++++++++++++++++--- 2 files changed, 68 insertions(+), 14 deletions(-) diff --git a/stream-core/src/main/java/org/dromara/streamquery/stream/core/stream/collector/Collective.java b/stream-core/src/main/java/org/dromara/streamquery/stream/core/stream/collector/Collective.java index 1fbf2e85..6401ea3e 100644 --- a/stream-core/src/main/java/org/dromara/streamquery/stream/core/stream/collector/Collective.java +++ b/stream-core/src/main/java/org/dromara/streamquery/stream/core/stream/collector/Collective.java @@ -116,28 +116,42 @@ public class Collective { CH_ID); } + /** + * Constructs a collector that retrieves a sublist from the input elements. + * + * @param limit The maximum length of the returned list. If null, returns all remaining elements. + * @param The type of the input elements. + * @return a {@code Collector} that collects all input elements into a {@code List}, representing a subset of the original collection. + */ public static Collector> limiting(int limit) { return subList(null, limit); } + /** + * Constructs a collector that retrieves a sublist from the input elements. + * + * @param skip The number of elements to skip, ignore the first few elements of the collection. If null, no elements are skipped. + * @param The type of the input elements. + * @return a {@code Collector} that collects all input elements into a {@code List}, representing a subset of the original collection. + */ public static Collector> skipping(int skip) { return subList(skip, null); } /** + * Constructs a collector that retrieves a sublist from the input elements. * - * @param skip - * @param limit - * @return a {@code Collector} which collects all the input elements into a {@code Set} - * @param the type of the input elements + * @param skip The number of elements to skip, ignore the first few elements of the collection. If null, no elements are skipped. + * @param limit The maximum length of the returned list. If null, returns all remaining elements. + * @param The type of the input elements. + * @return a {@code Collector} that collects all input elements into a {@code List}, representing a subset of the original collection. */ public static Collector> subList(Integer skip, Integer limit) { Integer offset = Opp.of(skip).orElse(0); - Integer limited = Opp.of(limit).orElse(0); return mapping(Function.identity(), collectingAndThen(toList(), list -> { int size = list.size(); - int fromIndex = offset > size ? size : offset; - int toIndex = Math.min(fromIndex + limited, size); + int fromIndex = Math.min(size, offset); + int toIndex = Opp.of(limit).map(l -> Math.min(fromIndex + l, size)).orElse(size); return list.subList(fromIndex, toIndex); })); } diff --git a/stream-core/src/test/java/org/dromara/streamquery/stream/core/stream/collector/CollectiveTest.java b/stream-core/src/test/java/org/dromara/streamquery/stream/core/stream/collector/CollectiveTest.java index 10231e45..3a4a1282 100644 --- a/stream-core/src/test/java/org/dromara/streamquery/stream/core/stream/collector/CollectiveTest.java +++ b/stream-core/src/test/java/org/dromara/streamquery/stream/core/stream/collector/CollectiveTest.java @@ -16,6 +16,7 @@ */ package org.dromara.streamquery.stream.core.stream.collector; +import org.dromara.streamquery.stream.core.lambda.LambdaInvokeException; import org.dromara.streamquery.stream.core.stream.Steam; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -62,12 +63,54 @@ class CollectiveTest { .isEmpty()); } + @Test + void testLimiting() { + List list = Arrays.asList(0, 0, 0, 1, 1, 1, 1); + Map> map = list.stream().collect(groupingBy(Function.identity(), limiting(0))); + Assertions.assertEquals(2, map.size()); + Assertions.assertEquals(0, map.get(0).size()); + Assertions.assertEquals(0, map.get(1).size()); + + Map> map1 = list.stream().collect(groupingBy(Function.identity(), limiting(2))); + Assertions.assertEquals(2, map1.size()); + Assertions.assertEquals(2, map1.get(0).size()); + Assertions.assertEquals(2, map1.get(1).size()); + + Map> map2 = list.stream().collect(groupingBy(Function.identity(), limiting(10))); + Assertions.assertEquals(2, map2.size()); + Assertions.assertEquals(3, map2.get(0).size()); + Assertions.assertEquals(4, map2.get(1).size()); + + Assertions.assertThrows(LambdaInvokeException.class, () -> list.stream().collect(groupingBy(Function.identity(), limiting(-1)))); + } + + @Test + void testSkipping() { + List list = Arrays.asList(0, 0, 0, 1, 1, 1, 1); + Map> map = list.stream().collect(groupingBy(Function.identity(), skipping(2))); + Assertions.assertEquals(2, map.size()); + Assertions.assertEquals(1, map.get(0).size()); + Assertions.assertEquals(2, map.get(1).size()); + + Map> map1 = list.stream().collect(groupingBy(Function.identity(), skipping(10))); + Assertions.assertEquals(2, map1.size()); + Assertions.assertEquals(0, map1.get(0).size()); + Assertions.assertEquals(0, map1.get(1).size()); + + Map> map2 = list.stream().collect(groupingBy(Function.identity(), skipping(0))); + Assertions.assertEquals(2, map2.size()); + Assertions.assertEquals(3, map2.get(0).size()); + Assertions.assertEquals(4, map2.get(1).size()); + + Assertions.assertThrows(LambdaInvokeException.class, () -> list.stream().collect(groupingBy(Function.identity(), skipping(-1)))); + } + @Test void testSubList() { - List list = Arrays.asList(0, 0, 0, 1, 1, 1, 2, 2, 2); + List list = Arrays.asList(0, 0, 1, 1, 1, 2, 2, 2, 2); Map> map1 = list.stream().collect(groupingBy(Function.identity(), subList(1, 2))); Assertions.assertEquals(3, map1.size()); - Assertions.assertEquals(2, map1.get(0).size()); + Assertions.assertEquals(1, map1.get(0).size()); Assertions.assertEquals(2, map1.get(1).size()); Assertions.assertEquals(2, map1.get(2).size()); @@ -77,11 +120,8 @@ class CollectiveTest { Assertions.assertEquals(0, map2.get(1).size()); Assertions.assertEquals(0, map2.get(2).size()); - - - Assertions.assertThrows(, () -> { - list.stream().collect(groupingBy(Function.identity(), subList(1, 0))); - }); + Assertions.assertThrows(LambdaInvokeException.class, () -> list.stream().collect(groupingBy(Function.identity(), subList(-1, 2)))); + Assertions.assertThrows(LambdaInvokeException.class, () -> list.stream().collect(groupingBy(Function.identity(), subList(1, -1)))); } @Test -- Gitee