From b1e9f263bcc2f9c8ba3861c6c1b84e3d5caf49ef Mon Sep 17 00:00:00 2001 From: huahua Date: Tue, 26 Jul 2022 10:07:15 +0800 Subject: [PATCH 01/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=92=88=E5=AF=B9=20in?= =?UTF-8?q?t=20long=20double=E7=B1=BB=E5=9E=8B=E7=9A=84sum=E5=92=8Cavg?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/stream/EasyStream.java | 69 +++++++++++++++++++ .../cn/hutool/core/stream/EasyStreamTest.java | 9 +++ 2 files changed, 78 insertions(+) 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 e3659496c..b84fefc03 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 @@ -5,6 +5,7 @@ import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ObjUtil; import java.util.Objects; +import java.util.OptionalDouble; import java.util.Spliterator; import java.util.function.*; import java.util.stream.Stream; @@ -264,6 +265,74 @@ public class EasyStream extends AbstractEnhancedWrappedStream(stream); } + + + + /** + * 计算int类型总和 + * + * @param mapper 映射 + * @return int + */ + public int sum(ToIntFunction mapper) { + return stream.mapToInt(mapper).sum(); + } + + /** + * 计算long类型总和 + * + * @param mapper 映射 + * @return long + */ + public long sum(ToLongFunction mapper) { + return stream.mapToLong(mapper).sum(); + } + + + /** + * 计算double总和 + * + * @param mapper 映射器 + * @return double + */ + public double sum(ToDoubleFunction mapper) { + return stream.mapToDouble(mapper).sum(); + } + + + + /** + * 计算int平均值 + * + * @param mapper 映射器 + * @return {@link Integer} + */ + public OptionalDouble avg(ToIntFunction mapper) { + return stream.mapToInt(mapper).average(); + } + + /** + * 计算double平均值 + * + * @param mapper 映射 + * @return {@link OptionalDouble} + */ + public OptionalDouble avg(ToDoubleFunction mapper) { + return stream.mapToDouble(mapper).average(); + } + + + /** + * 计算double平均值 + * + * @param mapper 映射 + * @return {@link OptionalDouble} + */ + public OptionalDouble avg(ToLongFunction mapper) { + return stream.mapToLong(mapper).average(); + } + + /** * 建造者 * 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 0f50b8337..1818f061f 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 @@ -433,4 +433,13 @@ public class EasyStreamTest { Assert.assertTrue(EasyStream.of(1).isNotEmpty()); } + + @Test + public void testIntSum() { + int sum = EasyStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).sum(Integer::intValue); + Assert.assertEquals(sum,55); + double doubleSum = EasyStream.of(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10).sum(Double::doubleValue); + Assert.assertEquals(doubleSum,59.6); + } + } From 835e7bf66d61ba33426ad32db48e014cd96353b9 Mon Sep 17 00:00:00 2001 From: huahua Date: Mon, 13 Feb 2023 21:09:53 +0800 Subject: [PATCH 02/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=92=88=E5=AF=B9=20bi?= =?UTF-8?q?gDecimal=E7=B1=BB=E5=9E=8B=E7=9A=84avg=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/stream/EasyStream.java | 35 ++++ .../cn/hutool/core/stream/EasyStreamTest.java | 161 +++++++++++------- 2 files changed, 135 insertions(+), 61 deletions(-) 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 b84fefc03..9001d19cf 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 @@ -1,13 +1,19 @@ package cn.hutool.core.stream; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.lang.Opt; +import cn.hutool.core.math.NumberUtil; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ObjUtil; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.List; import java.util.Objects; import java.util.OptionalDouble; import java.util.Spliterator; import java.util.function.*; +import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.stream.StreamSupport; @@ -299,6 +305,34 @@ public class EasyStream extends AbstractEnhancedWrappedStream mapper) { + return stream.map(mapper).reduce(BigDecimal.ZERO, BigDecimal::add); + } + + /** + * 计算bigDecimal平均值 + * @param mapper 映射 + * @param scale 精度 + * @param roundingMode 舍入模式 + * @return 如果元素的长度为0 那么会返回为空的opt + */ + public Opt avg(final Function mapper,int scale,RoundingMode roundingMode){ + //元素列表 + List bigDecimalList = stream.map(mapper).collect(Collectors.toList()); + if (CollUtil.isEmpty(bigDecimalList)){ + return Opt.ofNullable(null); + } + + return Opt.of(EasyStream.of(bigDecimalList).reduce(BigDecimal.ZERO, BigDecimal::add).divide(NumberUtil.toBigDecimal(bigDecimalList.size()),scale, roundingMode)); + } + + /** @@ -333,6 +367,7 @@ public class EasyStream extends AbstractEnhancedWrappedStream orders = Arrays.asList(1, 2, 3, 2); final List list = Arrays.asList("dromara", "guava", "sweet", "hutool"); final Map map = MapUtil.builder() - .put(1, "dromara") - .put(2, "hutool") - .put(3, "sweet") - .build(); + .put(1, "dromara") + .put(2, "hutool") + .put(3, "sweet") + .build(); final Map toZip = EasyStream.of(orders).toZip(list); Assert.assertEquals(map, toZip); @@ -122,15 +126,15 @@ public class EasyStreamTest { final List list = Arrays.asList(1, 2, 3); final Map> group = EasyStream.of(list).group(String::valueOf); Assert.assertEquals( - new HashMap>() { - private static final long serialVersionUID = 1L; + new HashMap>() { + private static final long serialVersionUID = 1L; - { - put("1", singletonList(1)); - put("2", singletonList(2)); - put("3", singletonList(3)); - } - }, group); + { + put("1", singletonList(1)); + put("2", singletonList(2)); + put("3", singletonList(3)); + } + }, group); } @Test @@ -140,7 +144,7 @@ public class EasyStreamTest { Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), mapIndex); // 并行流时正常 Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), - EasyStream.of("dromara", "hutool", "sweet").parallel().mapIdx((e, i) -> i + 1 + "." + e).toList()); + EasyStream.of("dromara", "hutool", "sweet").parallel().mapIdx((e, i) -> i + 1 + "." + e).toList()); } @Test @@ -181,7 +185,7 @@ public class EasyStreamTest { Assert.assertEquals(ListUtil.sort(collect2), ListUtil.sort(distinctBy2)); Assert.assertEquals( - 4, EasyStream.of(1, 2, 2, null, 3, null).parallel(true).distinct(t -> Objects.isNull(t) ? null : t.toString()).sequential().count() + 4, EasyStream.of(1, 2, 2, null, 3, null).parallel(true).distinct(t -> Objects.isNull(t) ? null : t.toString()).sequential().count() ); } @@ -217,28 +221,28 @@ public class EasyStreamTest { Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), mapIndex); // 并行流时正常 Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), - EasyStream.of("dromara", "hutool", "sweet").parallel() - .flatMapIdx((e, i) -> EasyStream.of(i + 1 + "." + e)).toList()); + EasyStream.of("dromara", "hutool", "sweet").parallel() + .flatMapIdx((e, i) -> EasyStream.of(i + 1 + "." + e)).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()); + .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()); + .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 @@ -274,7 +278,7 @@ public class EasyStreamTest { Assert.assertEquals(Arrays.asList("dromara", "hutool"), filterIndex); // 并行流时正常 Assert.assertEquals(Arrays.asList("dromara", "hutool"), - EasyStream.of("dromara", "hutool", "sweet").parallel().filterIdx((e, i) -> i < 2).toList()); + EasyStream.of("dromara", "hutool", "sweet").parallel().filterIdx((e, i) -> i < 2).toList()); } @Test @@ -380,23 +384,23 @@ public class EasyStreamTest { final List list = EasyStream.iterate(1, i -> i <= 10, i -> i + 1).toList(); final List res1 = EasyStream.of(list) - // 舍弃 5 - .takeWhile(e -> e < 5) - // 过滤奇数 - .filter(e -> (e & 1) == 0) - // 反序 - .sorted(Comparator.reverseOrder()) - .map(String::valueOf) - .toList(); + // 舍弃 5 + .takeWhile(e -> e < 5) + // 过滤奇数 + .filter(e -> (e & 1) == 0) + // 反序 + .sorted(Comparator.reverseOrder()) + .map(String::valueOf) + .toList(); Assert.assertEquals(Arrays.asList("4", "2"), res1); final List res2 = EasyStream.iterate(1, i -> i + 1) - .parallel() - .takeWhile(e -> e < 5) - .map(String::valueOf) - .map(Integer::valueOf) - .sorted(Comparator.naturalOrder()) - .toList(); + .parallel() + .takeWhile(e -> e < 5) + .map(String::valueOf) + .map(Integer::valueOf) + .sorted(Comparator.naturalOrder()) + .toList(); Assert.assertEquals(Arrays.asList(1, 2, 3, 4), res2); } @@ -406,25 +410,25 @@ public class EasyStreamTest { final List list = EasyStream.iterate(1, i -> i <= 10, i -> i + 1).toList(); final List res1 = EasyStream.of(list) - // 舍弃 5之前的数字 - .dropWhile(e -> e < 5) - // 过滤偶数 - .filter(e -> (e & 1) == 1) - // 反序 - .sorted(Comparator.reverseOrder()) - .map(String::valueOf) - .toList(); + // 舍弃 5之前的数字 + .dropWhile(e -> e < 5) + // 过滤偶数 + .filter(e -> (e & 1) == 1) + // 反序 + .sorted(Comparator.reverseOrder()) + .map(String::valueOf) + .toList(); Assert.assertEquals(Arrays.asList("9", "7", "5"), res1); final List res2 = EasyStream.of(list) - .parallel() - .dropWhile(e -> e < 5) - // 过滤偶数 - .filter(e -> (e & 1) == 1) - .map(String::valueOf) - .map(Integer::valueOf) - .sorted(Comparator.naturalOrder()) - .toList(); + .parallel() + .dropWhile(e -> e < 5) + // 过滤偶数 + .filter(e -> (e & 1) == 1) + .map(String::valueOf) + .map(Integer::valueOf) + .sorted(Comparator.naturalOrder()) + .toList(); Assert.assertEquals(Arrays.asList(5, 7, 9), res2); } @@ -435,11 +439,46 @@ public class EasyStreamTest { @Test - public void testIntSum() { + public void testIntSumAndAvg() { int sum = EasyStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).sum(Integer::intValue); - Assert.assertEquals(sum,55); - double doubleSum = EasyStream.of(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10).sum(Double::doubleValue); - Assert.assertEquals(doubleSum,59.6); + Assert.assertEquals(sum, 55); + + //测试为空 + List integerList = new ArrayList<>(); + int emptySum = EasyStream.of(integerList).sum(Integer::intValue); + Assert.assertEquals(emptySum, 0); } + @Test + public void testDoubleSumAndAvg() { + double doubleSum = EasyStream.of(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10).sum(Double::doubleValue); + Assert.assertEquals(doubleSum, 59.6,2); + + List doubleList = new ArrayList<>(); + double emptySum = EasyStream.of(doubleList).sum(Double::doubleValue); + Assert.assertEquals(emptySum, 0.0,2); + + } + + @Test + public void testLongSumAndAvg() { + long sum = EasyStream.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L).sum(Long::longValue); + Assert.assertEquals(sum, 55); + + List longList = new ArrayList<>(); + double emptySum = EasyStream.of(longList).sum(Long::longValue); + Assert.assertEquals(emptySum, 0L); + + } + + @Test + public void testBigDecimalSumAndAvg() { + BigDecimal sum = EasyStream.of(NumberUtil.toBigDecimal(1.1), NumberUtil.toBigDecimal(2.2), NumberUtil.toBigDecimal(3.3), NumberUtil.toBigDecimal(4.4), NumberUtil.toBigDecimal(5.5), NumberUtil.toBigDecimal(6.6), NumberUtil.toBigDecimal(7.7), NumberUtil.toBigDecimal(8.8), NumberUtil.toBigDecimal(9.9), NumberUtil.toBigDecimal(10.10)).sum(Function.identity()); + Assert.assertEquals(sum, 59.6); + + List bigDecimalList = new ArrayList<>(); + BigDecimal emptySum = EasyStream.of(bigDecimalList).sum(Function.identity()); + Assert.assertEquals(emptySum, BigDecimal.ZERO); + + } } From a079a809082e1e5cdd23502e7b377f0aaa65b032 Mon Sep 17 00:00:00 2001 From: LoveHuahua Date: Mon, 13 Feb 2023 21:48:23 +0800 Subject: [PATCH 03/10] =?UTF-8?q?=E8=A1=A5=E5=85=85=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/stream/EasyStream.java | 86 +++++--- .../cn/hutool/core/stream/EasyStreamTest.java | 196 ++++++++++++------ 2 files changed, 186 insertions(+), 96 deletions(-) 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 9001d19cf..1e2f4a919 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 @@ -104,7 +104,7 @@ public class EasyStream extends AbstractEnhancedWrappedStream 元素类型 * @return 包含单个元素的串行流 */ @@ -116,9 +116,9 @@ public class EasyStream extends AbstractEnhancedWrappedStream 元素类型 + * @param 元素类型 * @return 包含指定元素的串行流 - * 从一个安全数组中创建流 + * 从一个安全数组中创建流 */ @SafeVarargs @SuppressWarnings("varargs") @@ -130,7 +130,7 @@ public class EasyStream extends AbstractEnhancedWrappedStream 元素类型 + * @param 元素类型 * @return 流 */ public static EasyStream of(final Iterable iterable) { @@ -142,7 +142,7 @@ public class EasyStream extends AbstractEnhancedWrappedStream 元素类型 + * @param 元素类型 * @return 流 */ public static EasyStream of(final Iterable iterable, final boolean parallel) { @@ -157,7 +157,7 @@ public class EasyStream extends AbstractEnhancedWrappedStream 元素类型 + * @param 元素类型 * @return 流 */ public static EasyStream of(final Stream stream) { @@ -173,9 +173,9 @@ public class EasyStream extends AbstractEnhancedWrappedStream * - * @param 元素类型 + * @param 元素类型 * @param seed 初始值 - * @param f 用上一个元素作为参数执行并返回一个新的元素 + * @param f 用上一个元素作为参数执行并返回一个新的元素 * @return 无限有序流 */ public static EasyStream iterate(final T seed, final UnaryOperator f) { @@ -191,13 +191,14 @@ public class EasyStream extends AbstractEnhancedWrappedStream * - * @param 元素类型 - * @param seed 初始值 + * @param 元素类型 + * @param seed 初始值 * @param hasNext 条件值 - * @param next 用上一个元素作为参数执行并返回一个新的元素 + * @param next 用上一个元素作为参数执行并返回一个新的元素 * @return 无限有序流 */ - public static EasyStream iterate(final T seed, final Predicate hasNext, final UnaryOperator next) { + public static EasyStream iterate(final T seed, final Predicate hasNext, + final UnaryOperator next) { Objects.requireNonNull(next); Objects.requireNonNull(hasNext); return new EasyStream<>(StreamUtil.iterate(seed, hasNext, next)); @@ -209,7 +210,7 @@ public class EasyStream extends AbstractEnhancedWrappedStream 元素类型 - * @param s 用来生成元素的 {@code Supplier} + * @param s 用来生成元素的 {@code Supplier} * @return 无限串行无序流 */ public static EasyStream generate(final Supplier s) { @@ -224,8 +225,8 @@ public class EasyStream extends AbstractEnhancedWrappedStream从重复串行流进行拼接时可能会导致深度调用链甚至抛出 {@code StackOverflowException}

* * @param 元素类型 - * @param a 第一个流 - * @param b 第二个流 + * @param a 第一个流 + * @param b 第二个流 * @return 拼接两个流之后的流 */ public static EasyStream concat(final Stream a, final Stream b) { @@ -235,12 +236,13 @@ public class EasyStream extends AbstractEnhancedWrappedStream split(final CharSequence str, final String regex) { - return Opt.ofBlankAble(str).map(CharSequence::toString).map(s -> s.split(regex)).map(EasyStream::of).orElseGet(EasyStream::empty); + return Opt.ofBlankAble(str).map(CharSequence::toString).map(s -> s.split(regex)).map(EasyStream::of) + .orElseGet(EasyStream::empty); } // --------------------------------------------------------------- Static method end @@ -251,7 +253,7 @@ public class EasyStream extends AbstractEnhancedWrappedStream 函数执行后返回的类型 + * @param 函数执行后返回的类型 * @return 返回叠加操作后的流 */ @Override @@ -272,8 +274,6 @@ public class EasyStream extends AbstractEnhancedWrappedStream extends AbstractEnhancedWrappedStream mapper) { - return stream.map(mapper).reduce(BigDecimal.ZERO, BigDecimal::add); + return stream.map(mapper).reduce(BigDecimal.ZERO, BigDecimal::add); + } + + + /** + * 计算bigDecimal平均值 并以四舍五入的方式保留2位精度 + * + * @param mapper 映射 + * @return 如果元素的长度为0 那么会返回为空的opt + */ + public Opt avg(final Function mapper) { + return avg(mapper, 2); + } + + + /** + * 计算bigDecimal平均值 并以四舍五入的方式保留scale的进度 + * + * @param mapper 映射 + * @param scale 精度 + * @return 如果元素的长度为0 那么会返回为空的opt + */ + public Opt avg(final Function mapper, int scale) { + return avg(mapper, scale, RoundingMode.HALF_UP); } /** * 计算bigDecimal平均值 + * * @param mapper 映射 * @param scale 精度 * @param roundingMode 舍入模式 * @return 如果元素的长度为0 那么会返回为空的opt */ - public Opt avg(final Function mapper,int scale,RoundingMode roundingMode){ + public Opt avg(final Function mapper, int scale, RoundingMode roundingMode) { //元素列表 List bigDecimalList = stream.map(mapper).collect(Collectors.toList()); - if (CollUtil.isEmpty(bigDecimalList)){ + if (CollUtil.isEmpty(bigDecimalList)) { return Opt.ofNullable(null); } - return Opt.of(EasyStream.of(bigDecimalList).reduce(BigDecimal.ZERO, BigDecimal::add).divide(NumberUtil.toBigDecimal(bigDecimalList.size()),scale, roundingMode)); + return Opt.of(EasyStream.of(bigDecimalList).reduce(BigDecimal.ZERO, BigDecimal::add) + .divide(NumberUtil.toBigDecimal(bigDecimalList.size()), scale, roundingMode)); } - - /** * 计算int平均值 * @@ -367,7 +390,6 @@ public class EasyStream extends AbstractEnhancedWrappedStream extends AbstractEnhancedWrappedStream{@code - * accept(t) - * return this; - * } + *
{@code
+		 * 						    accept(t)
+		 * 						    return this;
+		 *                      }
*/ default Builder add(final T t) { accept(t); 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 5b83598a9..f44a0570a 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 @@ -2,8 +2,10 @@ package cn.hutool.core.stream; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.ListUtil; +import cn.hutool.core.lang.Opt; import cn.hutool.core.map.MapUtil; import cn.hutool.core.math.NumberUtil; +import java.math.RoundingMode; import org.junit.Assert; import org.junit.Test; @@ -85,10 +87,10 @@ public class EasyStreamTest { final List orders = Arrays.asList(1, 2, 3, 2); final List list = Arrays.asList("dromara", "guava", "sweet", "hutool"); final Map map = MapUtil.builder() - .put(1, "dromara") - .put(2, "hutool") - .put(3, "sweet") - .build(); + .put(1, "dromara") + .put(2, "hutool") + .put(3, "sweet") + .build(); final Map toZip = EasyStream.of(orders).toZip(list); Assert.assertEquals(map, toZip); @@ -126,15 +128,15 @@ public class EasyStreamTest { final List list = Arrays.asList(1, 2, 3); final Map> group = EasyStream.of(list).group(String::valueOf); Assert.assertEquals( - new HashMap>() { - private static final long serialVersionUID = 1L; + new HashMap>() { + private static final long serialVersionUID = 1L; - { - put("1", singletonList(1)); - put("2", singletonList(2)); - put("3", singletonList(3)); - } - }, group); + { + put("1", singletonList(1)); + put("2", singletonList(2)); + put("3", singletonList(3)); + } + }, group); } @Test @@ -144,7 +146,7 @@ public class EasyStreamTest { Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), mapIndex); // 并行流时正常 Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), - EasyStream.of("dromara", "hutool", "sweet").parallel().mapIdx((e, i) -> i + 1 + "." + e).toList()); + EasyStream.of("dromara", "hutool", "sweet").parallel().mapIdx((e, i) -> i + 1 + "." + e).toList()); } @Test @@ -185,7 +187,8 @@ public class EasyStreamTest { Assert.assertEquals(ListUtil.sort(collect2), ListUtil.sort(distinctBy2)); Assert.assertEquals( - 4, EasyStream.of(1, 2, 2, null, 3, null).parallel(true).distinct(t -> Objects.isNull(t) ? null : t.toString()).sequential().count() + 4, EasyStream.of(1, 2, 2, null, 3, null).parallel(true) + .distinct(t -> Objects.isNull(t) ? null : t.toString()).sequential().count() ); } @@ -221,28 +224,28 @@ public class EasyStreamTest { Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), mapIndex); // 并行流时正常 Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), - EasyStream.of("dromara", "hutool", "sweet").parallel() - .flatMapIdx((e, i) -> EasyStream.of(i + 1 + "." + e)).toList()); + EasyStream.of("dromara", "hutool", "sweet").parallel() + .flatMapIdx((e, i) -> EasyStream.of(i + 1 + "." + e)).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()); + .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()); + .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 @@ -278,7 +281,7 @@ public class EasyStreamTest { Assert.assertEquals(Arrays.asList("dromara", "hutool"), filterIndex); // 并行流时正常 Assert.assertEquals(Arrays.asList("dromara", "hutool"), - EasyStream.of("dromara", "hutool", "sweet").parallel().filterIdx((e, i) -> i < 2).toList()); + EasyStream.of("dromara", "hutool", "sweet").parallel().filterIdx((e, i) -> i < 2).toList()); } @Test @@ -384,23 +387,23 @@ public class EasyStreamTest { final List list = EasyStream.iterate(1, i -> i <= 10, i -> i + 1).toList(); final List res1 = EasyStream.of(list) - // 舍弃 5 - .takeWhile(e -> e < 5) - // 过滤奇数 - .filter(e -> (e & 1) == 0) - // 反序 - .sorted(Comparator.reverseOrder()) - .map(String::valueOf) - .toList(); + // 舍弃 5 + .takeWhile(e -> e < 5) + // 过滤奇数 + .filter(e -> (e & 1) == 0) + // 反序 + .sorted(Comparator.reverseOrder()) + .map(String::valueOf) + .toList(); Assert.assertEquals(Arrays.asList("4", "2"), res1); final List res2 = EasyStream.iterate(1, i -> i + 1) - .parallel() - .takeWhile(e -> e < 5) - .map(String::valueOf) - .map(Integer::valueOf) - .sorted(Comparator.naturalOrder()) - .toList(); + .parallel() + .takeWhile(e -> e < 5) + .map(String::valueOf) + .map(Integer::valueOf) + .sorted(Comparator.naturalOrder()) + .toList(); Assert.assertEquals(Arrays.asList(1, 2, 3, 4), res2); } @@ -410,25 +413,25 @@ public class EasyStreamTest { final List list = EasyStream.iterate(1, i -> i <= 10, i -> i + 1).toList(); final List res1 = EasyStream.of(list) - // 舍弃 5之前的数字 - .dropWhile(e -> e < 5) - // 过滤偶数 - .filter(e -> (e & 1) == 1) - // 反序 - .sorted(Comparator.reverseOrder()) - .map(String::valueOf) - .toList(); + // 舍弃 5之前的数字 + .dropWhile(e -> e < 5) + // 过滤偶数 + .filter(e -> (e & 1) == 1) + // 反序 + .sorted(Comparator.reverseOrder()) + .map(String::valueOf) + .toList(); Assert.assertEquals(Arrays.asList("9", "7", "5"), res1); final List res2 = EasyStream.of(list) - .parallel() - .dropWhile(e -> e < 5) - // 过滤偶数 - .filter(e -> (e & 1) == 1) - .map(String::valueOf) - .map(Integer::valueOf) - .sorted(Comparator.naturalOrder()) - .toList(); + .parallel() + .dropWhile(e -> e < 5) + // 过滤偶数 + .filter(e -> (e & 1) == 1) + .map(String::valueOf) + .map(Integer::valueOf) + .sorted(Comparator.naturalOrder()) + .toList(); Assert.assertEquals(Arrays.asList(5, 7, 9), res2); } @@ -440,6 +443,7 @@ public class EasyStreamTest { @Test public void testIntSumAndAvg() { + //测试int类型的总和 int sum = EasyStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).sum(Integer::intValue); Assert.assertEquals(sum, 55); @@ -447,38 +451,102 @@ public class EasyStreamTest { List integerList = new ArrayList<>(); int emptySum = EasyStream.of(integerList).sum(Integer::intValue); Assert.assertEquals(emptySum, 0); + + //测试平均值 + OptionalDouble avg = EasyStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).avg(Integer::intValue); + Assert.assertEquals(avg.getAsDouble(), 5.5, 2); + + //测试元素为空 + OptionalDouble emptyAvg = EasyStream.of(integerList).avg(Integer::intValue); + Assert.assertFalse(emptyAvg.isPresent()); + } @Test public void testDoubleSumAndAvg() { + //测试double类型的sum double doubleSum = EasyStream.of(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10).sum(Double::doubleValue); - Assert.assertEquals(doubleSum, 59.6,2); + Assert.assertEquals(doubleSum, 59.6, 2); + //测试double类型的sum 无元素double List doubleList = new ArrayList<>(); double emptySum = EasyStream.of(doubleList).sum(Double::doubleValue); - Assert.assertEquals(emptySum, 0.0,2); + Assert.assertEquals(emptySum, 0.0, 2); + + //测试double类型的avg + OptionalDouble doubleAvg = EasyStream.of(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10) + .avg(Double::doubleValue); + Assert.assertEquals(doubleAvg.getAsDouble(), 5.96, 2); + + //测试double类型的 空元素的avg + OptionalDouble emptyDoubleAvg = EasyStream.of(doubleList).avg(Double::doubleValue); + Assert.assertFalse(emptyDoubleAvg.isPresent()); } @Test public void testLongSumAndAvg() { + //测试long类型的sum long sum = EasyStream.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L).sum(Long::longValue); Assert.assertEquals(sum, 55); + //测试long类型的空元素 sum List longList = new ArrayList<>(); - double emptySum = EasyStream.of(longList).sum(Long::longValue); + long emptySum = EasyStream.of(longList).sum(Long::longValue); Assert.assertEquals(emptySum, 0L); + //测试long类型的avg + OptionalDouble doubleAvg = EasyStream.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L).avg(Long::longValue); + Assert.assertEquals(doubleAvg.getAsDouble(), 5.5, 2); + + //测试long类型的avg 空元素 + OptionalDouble emptyDoubleAvg = EasyStream.of(longList).avg(Long::longValue); + Assert.assertFalse(emptyDoubleAvg.isPresent()); + + } @Test public void testBigDecimalSumAndAvg() { - BigDecimal sum = EasyStream.of(NumberUtil.toBigDecimal(1.1), NumberUtil.toBigDecimal(2.2), NumberUtil.toBigDecimal(3.3), NumberUtil.toBigDecimal(4.4), NumberUtil.toBigDecimal(5.5), NumberUtil.toBigDecimal(6.6), NumberUtil.toBigDecimal(7.7), NumberUtil.toBigDecimal(8.8), NumberUtil.toBigDecimal(9.9), NumberUtil.toBigDecimal(10.10)).sum(Function.identity()); - Assert.assertEquals(sum, 59.6); + //测试bigDecimal的sum + BigDecimal sum = EasyStream.of(NumberUtil.toBigDecimal(1.1), NumberUtil.toBigDecimal(2.2), + NumberUtil.toBigDecimal(3.3), NumberUtil.toBigDecimal(4.4), NumberUtil.toBigDecimal(5.5), + NumberUtil.toBigDecimal(6.6), NumberUtil.toBigDecimal(7.7), NumberUtil.toBigDecimal(8.8), + NumberUtil.toBigDecimal(9.9), NumberUtil.toBigDecimal(10.10)).sum(Function.identity()); + Assert.assertEquals(sum, NumberUtil.toBigDecimal(59.6)); - List bigDecimalList = new ArrayList<>(); - BigDecimal emptySum = EasyStream.of(bigDecimalList).sum(Function.identity()); + //测试bigDecimal的sum 空元素 + List bigDecimalEmptyList = new ArrayList<>(); + BigDecimal emptySum = EasyStream.of(bigDecimalEmptyList).sum(Function.identity()); Assert.assertEquals(emptySum, BigDecimal.ZERO); + //测试bigDecimal的avg + Opt bigDecimalAvgFullParam = EasyStream.of(NumberUtil.toBigDecimal(1.1), + NumberUtil.toBigDecimal(2.2), NumberUtil.toBigDecimal(3.3), NumberUtil.toBigDecimal(4.4), + NumberUtil.toBigDecimal(5.5), NumberUtil.toBigDecimal(6.6), NumberUtil.toBigDecimal(7.7), + NumberUtil.toBigDecimal(8.8), NumberUtil.toBigDecimal(9.9), NumberUtil.toBigDecimal(10.10)) + .avg(Function.identity(), 2, RoundingMode.HALF_UP); + Assert.assertEquals(bigDecimalAvgFullParam.get(), NumberUtil.toBigDecimal(5.96)); + + Opt bigDecimalAvgOneParam = EasyStream.of(NumberUtil.toBigDecimal(1.1), + NumberUtil.toBigDecimal(2.2), NumberUtil.toBigDecimal(3.3), NumberUtil.toBigDecimal(4.4), + NumberUtil.toBigDecimal(5.5), NumberUtil.toBigDecimal(6.6), NumberUtil.toBigDecimal(7.7), + NumberUtil.toBigDecimal(8.8), NumberUtil.toBigDecimal(9.9), NumberUtil.toBigDecimal(10.10)) + .avg(Function.identity()); + Assert.assertEquals(bigDecimalAvgOneParam.get(), NumberUtil.toBigDecimal(5.96)); + + Opt bigDecimalAvgTwoParam = EasyStream.of(NumberUtil.toBigDecimal(1.1), + NumberUtil.toBigDecimal(2.2), NumberUtil.toBigDecimal(3.3), NumberUtil.toBigDecimal(4.4), + NumberUtil.toBigDecimal(5.5), NumberUtil.toBigDecimal(6.6), NumberUtil.toBigDecimal(7.7), + NumberUtil.toBigDecimal(8.8), NumberUtil.toBigDecimal(9.9), NumberUtil.toBigDecimal(10.10)) + .avg(Function.identity(), 2); + Assert.assertEquals(bigDecimalAvgTwoParam.get(), NumberUtil.toBigDecimal(5.96)); + + //测试bigDecimal的avg 空元素 + Opt emptyBigDecimalAvg = EasyStream.of(bigDecimalEmptyList) + .avg(Function.identity(), 2, RoundingMode.HALF_UP); + Assert.assertFalse(emptyBigDecimalAvg.isPresent()); + + } } From ef61893d59386f96b6ff2894a828eb6b15eda258 Mon Sep 17 00:00:00 2001 From: LoveHuahua Date: Mon, 13 Feb 2023 22:04:34 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E6=9B=B4=E6=AD=A3=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E8=BF=94=E5=9B=9E=E7=B1=BB=E5=9E=8B=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/stream/EasyStream.java | 14 ++++---- .../cn/hutool/core/stream/EasyStreamTest.java | 35 +++++++++++-------- 2 files changed, 27 insertions(+), 22 deletions(-) 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 1e2f4a919..549911bc5 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 @@ -320,7 +320,7 @@ public class EasyStream extends AbstractEnhancedWrappedStream 如果元素的长度为0 那么会返回为空的opt */ public Opt avg(final Function mapper) { return avg(mapper, 2); @@ -332,7 +332,7 @@ public class EasyStream extends AbstractEnhancedWrappedStream */ public Opt avg(final Function mapper, int scale) { return avg(mapper, scale, RoundingMode.HALF_UP); @@ -344,7 +344,7 @@ public class EasyStream extends AbstractEnhancedWrappedStream 如果元素的长度为0 那么会返回为空的opt */ public Opt avg(final Function mapper, int scale, RoundingMode roundingMode) { //元素列表 @@ -362,7 +362,7 @@ public class EasyStream extends AbstractEnhancedWrappedStream mapper) { return stream.mapToInt(mapper).average(); @@ -406,9 +406,9 @@ public class EasyStream extends AbstractEnhancedWrappedStream{@code - * accept(t) - * return this; - * } + * accept(t) + * return this; + * } */ default Builder add(final T t) { accept(t); 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 f44a0570a..5646d772f 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 @@ -445,16 +445,17 @@ public class EasyStreamTest { public void testIntSumAndAvg() { //测试int类型的总和 int sum = EasyStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).sum(Integer::intValue); - Assert.assertEquals(sum, 55); + Assert.assertEquals(55, sum); //测试为空 List integerList = new ArrayList<>(); int emptySum = EasyStream.of(integerList).sum(Integer::intValue); - Assert.assertEquals(emptySum, 0); + Assert.assertEquals(0, emptySum); //测试平均值 OptionalDouble avg = EasyStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).avg(Integer::intValue); - Assert.assertEquals(avg.getAsDouble(), 5.5, 2); + Assert.assertTrue(avg.isPresent()); + Assert.assertEquals(5.5, avg.getAsDouble(), 2); //测试元素为空 OptionalDouble emptyAvg = EasyStream.of(integerList).avg(Integer::intValue); @@ -466,17 +467,18 @@ public class EasyStreamTest { public void testDoubleSumAndAvg() { //测试double类型的sum double doubleSum = EasyStream.of(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10).sum(Double::doubleValue); - Assert.assertEquals(doubleSum, 59.6, 2); + Assert.assertEquals(59.6, doubleSum, 2); //测试double类型的sum 无元素double List doubleList = new ArrayList<>(); double emptySum = EasyStream.of(doubleList).sum(Double::doubleValue); - Assert.assertEquals(emptySum, 0.0, 2); + Assert.assertEquals(0.0, emptySum, 2); //测试double类型的avg OptionalDouble doubleAvg = EasyStream.of(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10) .avg(Double::doubleValue); - Assert.assertEquals(doubleAvg.getAsDouble(), 5.96, 2); + Assert.assertTrue(doubleAvg.isPresent()); + Assert.assertEquals(5.96, doubleAvg.getAsDouble(), 2); //测试double类型的 空元素的avg OptionalDouble emptyDoubleAvg = EasyStream.of(doubleList).avg(Double::doubleValue); @@ -488,16 +490,17 @@ public class EasyStreamTest { public void testLongSumAndAvg() { //测试long类型的sum long sum = EasyStream.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L).sum(Long::longValue); - Assert.assertEquals(sum, 55); + Assert.assertEquals(55, sum); //测试long类型的空元素 sum List longList = new ArrayList<>(); long emptySum = EasyStream.of(longList).sum(Long::longValue); - Assert.assertEquals(emptySum, 0L); + Assert.assertEquals(0L, emptySum); //测试long类型的avg OptionalDouble doubleAvg = EasyStream.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L).avg(Long::longValue); - Assert.assertEquals(doubleAvg.getAsDouble(), 5.5, 2); + Assert.assertTrue(doubleAvg.isPresent()); + Assert.assertEquals(5.5, doubleAvg.getAsDouble(), 2); //测试long类型的avg 空元素 OptionalDouble emptyDoubleAvg = EasyStream.of(longList).avg(Long::longValue); @@ -513,34 +516,36 @@ public class EasyStreamTest { NumberUtil.toBigDecimal(3.3), NumberUtil.toBigDecimal(4.4), NumberUtil.toBigDecimal(5.5), NumberUtil.toBigDecimal(6.6), NumberUtil.toBigDecimal(7.7), NumberUtil.toBigDecimal(8.8), NumberUtil.toBigDecimal(9.9), NumberUtil.toBigDecimal(10.10)).sum(Function.identity()); - Assert.assertEquals(sum, NumberUtil.toBigDecimal(59.6)); + Assert.assertEquals(NumberUtil.toBigDecimal(59.6), sum); //测试bigDecimal的sum 空元素 List bigDecimalEmptyList = new ArrayList<>(); BigDecimal emptySum = EasyStream.of(bigDecimalEmptyList).sum(Function.identity()); - Assert.assertEquals(emptySum, BigDecimal.ZERO); + Assert.assertEquals(BigDecimal.ZERO, emptySum); - //测试bigDecimal的avg + //测试bigDecimal的avg全参 Opt bigDecimalAvgFullParam = EasyStream.of(NumberUtil.toBigDecimal(1.1), NumberUtil.toBigDecimal(2.2), NumberUtil.toBigDecimal(3.3), NumberUtil.toBigDecimal(4.4), NumberUtil.toBigDecimal(5.5), NumberUtil.toBigDecimal(6.6), NumberUtil.toBigDecimal(7.7), NumberUtil.toBigDecimal(8.8), NumberUtil.toBigDecimal(9.9), NumberUtil.toBigDecimal(10.10)) .avg(Function.identity(), 2, RoundingMode.HALF_UP); - Assert.assertEquals(bigDecimalAvgFullParam.get(), NumberUtil.toBigDecimal(5.96)); + Assert.assertEquals(NumberUtil.toBigDecimal(5.96), bigDecimalAvgFullParam.get()); + //测试bigDecimal的avg双参 Opt bigDecimalAvgOneParam = EasyStream.of(NumberUtil.toBigDecimal(1.1), NumberUtil.toBigDecimal(2.2), NumberUtil.toBigDecimal(3.3), NumberUtil.toBigDecimal(4.4), NumberUtil.toBigDecimal(5.5), NumberUtil.toBigDecimal(6.6), NumberUtil.toBigDecimal(7.7), NumberUtil.toBigDecimal(8.8), NumberUtil.toBigDecimal(9.9), NumberUtil.toBigDecimal(10.10)) .avg(Function.identity()); - Assert.assertEquals(bigDecimalAvgOneParam.get(), NumberUtil.toBigDecimal(5.96)); + Assert.assertEquals(NumberUtil.toBigDecimal(5.96), bigDecimalAvgOneParam.get()); + //测试bigDecimal的avg单参 Opt bigDecimalAvgTwoParam = EasyStream.of(NumberUtil.toBigDecimal(1.1), NumberUtil.toBigDecimal(2.2), NumberUtil.toBigDecimal(3.3), NumberUtil.toBigDecimal(4.4), NumberUtil.toBigDecimal(5.5), NumberUtil.toBigDecimal(6.6), NumberUtil.toBigDecimal(7.7), NumberUtil.toBigDecimal(8.8), NumberUtil.toBigDecimal(9.9), NumberUtil.toBigDecimal(10.10)) .avg(Function.identity(), 2); - Assert.assertEquals(bigDecimalAvgTwoParam.get(), NumberUtil.toBigDecimal(5.96)); + Assert.assertEquals(NumberUtil.toBigDecimal(5.96), bigDecimalAvgTwoParam.get()); //测试bigDecimal的avg 空元素 Opt emptyBigDecimalAvg = EasyStream.of(bigDecimalEmptyList) From c6d6333cbcd3fc00f6620ef486ab5593cadc59a7 Mon Sep 17 00:00:00 2001 From: LoveHuahua Date: Mon, 13 Feb 2023 22:09:06 +0800 Subject: [PATCH 05/10] =?UTF-8?q?=E6=9B=B4=E6=AD=A3=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E8=BF=94=E5=9B=9E=E7=B1=BB=E5=9E=8B?= 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 549911bc5..d5642c9cc 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 @@ -309,7 +309,7 @@ public class EasyStream extends AbstractEnhancedWrappedStream mapper) { return stream.map(mapper).reduce(BigDecimal.ZERO, BigDecimal::add); From 30922016349034dfa419032171dcfa14165d7d2d Mon Sep 17 00:00:00 2001 From: LoveHuahua Date: Sun, 19 Feb 2023 15:14:06 +0800 Subject: [PATCH 06/10] =?UTF-8?q?=E6=8C=89=E7=85=A7=E8=B6=85=E5=93=A5?= =?UTF-8?q?=E6=95=99=E7=9A=84=20=E4=BD=BF=E7=94=A8number=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?decimal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/cn/hutool/core/stream/EasyStream.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 d5642c9cc..fdd2d390c 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 @@ -306,13 +306,13 @@ public class EasyStream extends AbstractEnhancedWrappedStream mapper) { - return stream.map(mapper).reduce(BigDecimal.ZERO, BigDecimal::add); + public BigDecimal sum(final Function mapper) { + return stream.map(mapper).reduce(BigDecimal.ZERO, NumberUtil::add,NumberUtil::add); } From ec86b8ed443c4a6077d969b98beec6a4a77b99c1 Mon Sep 17 00:00:00 2001 From: VampireAchao Date: Sun, 19 Feb 2023 20:03:42 +0800 Subject: [PATCH 07/10] =?UTF-8?q?=E4=BF=AE=E5=A4=8Djavadoc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/cn/hutool/core/stream/EasyStream.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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 fdd2d390c..032c4fd3b 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 @@ -309,6 +309,7 @@ public class EasyStream extends AbstractEnhancedWrappedStream 映射后的类型 * @return {@link BigDecimal} */ public BigDecimal sum(final Function mapper) { @@ -320,7 +321,7 @@ public class EasyStream extends AbstractEnhancedWrappedStream 如果元素的长度为0 那么会返回为空的opt + * @return 计算结果 如果元素的长度为0 那么会返回为空的opt */ public Opt avg(final Function mapper) { return avg(mapper, 2); @@ -332,7 +333,7 @@ public class EasyStream extends AbstractEnhancedWrappedStream + * @return 计算结果 如果元素的长度为0 那么会返回为空的opt */ public Opt avg(final Function mapper, int scale) { return avg(mapper, scale, RoundingMode.HALF_UP); @@ -344,7 +345,7 @@ public class EasyStream extends AbstractEnhancedWrappedStream 如果元素的长度为0 那么会返回为空的opt + * @return 计算结果 如果元素的长度为0 那么会返回为空的opt */ public Opt avg(final Function mapper, int scale, RoundingMode roundingMode) { //元素列表 From 5f97b3d47ca703b293fd5775e7cadbd3b2bfc196 Mon Sep 17 00:00:00 2001 From: emptypoint <1215582715@qq.com> Date: Tue, 28 Feb 2023 02:19:49 +0000 Subject: [PATCH 08/10] Update hutool-core/src/main/java/cn/hutool/core/stream/EasyStream.java --- 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 032c4fd3b..27adf6b78 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 @@ -210,7 +210,7 @@ public class EasyStream extends AbstractEnhancedWrappedStream 元素类型 - * @param s 用来生成元素的 {@code Supplier} + * @param s 用来生成元素的 {@link Supplier} * @return 无限串行无序流 */ public static EasyStream generate(final Supplier s) { From d89541364f3f8e6e915d2b6525dd34ed334dd0bd Mon Sep 17 00:00:00 2001 From: LoveHuahua Date: Tue, 28 Feb 2023 10:59:49 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=87=E6=A1=A3=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9final=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/stream/EasyStream.java | 85 ++++++++++--------- .../cn/hutool/core/stream/EasyStreamTest.java | 55 ++++++------ 2 files changed, 71 insertions(+), 69 deletions(-) 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 fdd2d390c..51a46c18c 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 @@ -275,52 +275,52 @@ public class EasyStream extends AbstractEnhancedWrappedStream mapper) { + public int sum(final ToIntFunction mapper) { return stream.mapToInt(mapper).sum(); } /** - * 计算long类型总和 + * 计算long类型的总和 * - * @param mapper 映射 - * @return long + * @param mapper 将对象转换为long的 {@link Function} + * @return long 总和 */ - public long sum(ToLongFunction mapper) { + public long sum(final ToLongFunction mapper) { return stream.mapToLong(mapper).sum(); } /** - * 计算double总和 + * 计算double类型的总和 * - * @param mapper 映射器 - * @return double + * @param mapper 将对象转换为double的 {@link Function} + * @return double 总和 */ - public double sum(ToDoubleFunction mapper) { + public double sum(final ToDoubleFunction mapper) { return stream.mapToDouble(mapper).sum(); } /** - * 计算number的总和 + * 计算 {@link Number} 类型的总和 * - * @param mapper 映射 - * @return {@link BigDecimal} + * @param mapper 将对象转换为{@link Number} 的 {@link Function} + * @return {@link BigDecimal} , 如果流为空, 返回 {@link BigDecimal#ZERO} */ public BigDecimal sum(final Function mapper) { - return stream.map(mapper).reduce(BigDecimal.ZERO, NumberUtil::add,NumberUtil::add); + return stream.map(mapper).reduce(BigDecimal.ZERO, NumberUtil::add, NumberUtil::add); } /** - * 计算bigDecimal平均值 并以四舍五入的方式保留2位精度 + * 计算 {@link BigDecimal} 类型的平均值 并以四舍五入的方式保留2位精度 * - * @param mapper 映射 - * @return {@link Opt}<{@link BigDecimal}> 如果元素的长度为0 那么会返回为空的opt + * @param mapper 将对象转换为{@link BigDecimal}的 {@link Function} + * @return {@link Opt}<{@link BigDecimal}>; 如果流的长度为0, 返回 {@link Opt#empty()} */ public Opt avg(final Function mapper) { return avg(mapper, 2); @@ -328,53 +328,54 @@ public class EasyStream extends AbstractEnhancedWrappedStream + * @param mapper 将对象转换为{@link BigDecimal} 的 {@link Function} + * @param scale 保留精度 + * @return {@link Opt}<{@link BigDecimal}> 如果流的长度为0, 返回 {@link Opt#empty()} */ - public Opt avg(final Function mapper, int scale) { + public Opt avg(final Function mapper, final int scale) { return avg(mapper, scale, RoundingMode.HALF_UP); } /** - * 计算bigDecimal平均值 + * 计算 {@link BigDecimal} 类型的平均值 * - * @param mapper 映射 - * @param scale 精度 + * @param mapper 将对象转换为{@link BigDecimal} 的 {@link Function} + * @param scale 保留精度 * @param roundingMode 舍入模式 - * @return {@link Opt}<{@link BigDecimal}> 如果元素的长度为0 那么会返回为空的opt + * @return {@link Opt}<{@link BigDecimal}> 如果元素的长度为0 那么会返回 {@link Opt#empty()} */ - public Opt avg(final Function mapper, int scale, RoundingMode roundingMode) { + public Opt avg(final Function mapper, final int scale, + final RoundingMode roundingMode) { //元素列表 List bigDecimalList = stream.map(mapper).collect(Collectors.toList()); if (CollUtil.isEmpty(bigDecimalList)) { - return Opt.ofNullable(null); + return Opt.empty(); } - return Opt.of(EasyStream.of(bigDecimalList).reduce(BigDecimal.ZERO, BigDecimal::add) + return Opt.ofNullable(EasyStream.of(bigDecimalList).reduce(BigDecimal.ZERO, BigDecimal::add) .divide(NumberUtil.toBigDecimal(bigDecimalList.size()), scale, roundingMode)); } /** - * 计算int平均值 + * 计算int类型的平均值 * - * @param mapper 映射器 - * @return {@link OptionalDouble} + * @param mapper 将对象转换为int 的 {@link Function} + * @return {@link OptionalDouble} 如果流的长度为0 那么会返回{@link OptionalDouble#empty()} */ - public OptionalDouble avg(ToIntFunction mapper) { + public OptionalDouble avg(final ToIntFunction mapper) { return stream.mapToInt(mapper).average(); } /** - * 计算double平均值 + * 计算double类型的平均值 * - * @param mapper 映射 - * @return {@link OptionalDouble} + * @param mapper 将对象转换为double 的 {@link Function} + * @return {@link OptionalDouble} 如果流的长度为0 那么会返回{@link OptionalDouble#empty()} */ - public OptionalDouble avg(ToDoubleFunction mapper) { + public OptionalDouble avg(final ToDoubleFunction mapper) { return stream.mapToDouble(mapper).average(); } @@ -382,10 +383,10 @@ public class EasyStream extends AbstractEnhancedWrappedStream mapper) { + public OptionalDouble avg(final ToLongFunction mapper) { return stream.mapToLong(mapper).average(); } 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 5646d772f..2fa800d3a 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 @@ -1,22 +1,31 @@ package cn.hutool.core.stream; -import cn.hutool.core.collection.CollUtil; +import static java.util.Collections.singletonList; + import cn.hutool.core.collection.ListUtil; import cn.hutool.core.lang.Opt; import cn.hutool.core.map.MapUtil; import cn.hutool.core.math.NumberUtil; -import java.math.RoundingMode; -import org.junit.Assert; -import org.junit.Test; - import java.math.BigDecimal; -import java.util.*; +import java.math.RoundingMode; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.OptionalDouble; +import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; - -import static java.util.Collections.singletonList; +import org.junit.Assert; +import org.junit.Test; /** * @author VampireAchao @@ -490,7 +499,7 @@ public class EasyStreamTest { public void testLongSumAndAvg() { //测试long类型的sum long sum = EasyStream.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L).sum(Long::longValue); - Assert.assertEquals(55, sum); + Assert.assertEquals(55L, sum); //测试long类型的空元素 sum List longList = new ArrayList<>(); @@ -512,10 +521,8 @@ public class EasyStreamTest { @Test public void testBigDecimalSumAndAvg() { //测试bigDecimal的sum - BigDecimal sum = EasyStream.of(NumberUtil.toBigDecimal(1.1), NumberUtil.toBigDecimal(2.2), - NumberUtil.toBigDecimal(3.3), NumberUtil.toBigDecimal(4.4), NumberUtil.toBigDecimal(5.5), - NumberUtil.toBigDecimal(6.6), NumberUtil.toBigDecimal(7.7), NumberUtil.toBigDecimal(8.8), - NumberUtil.toBigDecimal(9.9), NumberUtil.toBigDecimal(10.10)).sum(Function.identity()); + BigDecimal sum = EasyStream.of(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10).map(NumberUtil::toBigDecimal) + .sum(Function.identity()); Assert.assertEquals(NumberUtil.toBigDecimal(59.6), sum); //测试bigDecimal的sum 空元素 @@ -524,26 +531,20 @@ public class EasyStreamTest { Assert.assertEquals(BigDecimal.ZERO, emptySum); //测试bigDecimal的avg全参 - Opt bigDecimalAvgFullParam = EasyStream.of(NumberUtil.toBigDecimal(1.1), - NumberUtil.toBigDecimal(2.2), NumberUtil.toBigDecimal(3.3), NumberUtil.toBigDecimal(4.4), - NumberUtil.toBigDecimal(5.5), NumberUtil.toBigDecimal(6.6), NumberUtil.toBigDecimal(7.7), - NumberUtil.toBigDecimal(8.8), NumberUtil.toBigDecimal(9.9), NumberUtil.toBigDecimal(10.10)) + Opt bigDecimalAvgFullParam = EasyStream.of(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10) + .map(NumberUtil::toBigDecimal) .avg(Function.identity(), 2, RoundingMode.HALF_UP); Assert.assertEquals(NumberUtil.toBigDecimal(5.96), bigDecimalAvgFullParam.get()); - //测试bigDecimal的avg双参 - Opt bigDecimalAvgOneParam = EasyStream.of(NumberUtil.toBigDecimal(1.1), - NumberUtil.toBigDecimal(2.2), NumberUtil.toBigDecimal(3.3), NumberUtil.toBigDecimal(4.4), - NumberUtil.toBigDecimal(5.5), NumberUtil.toBigDecimal(6.6), NumberUtil.toBigDecimal(7.7), - NumberUtil.toBigDecimal(8.8), NumberUtil.toBigDecimal(9.9), NumberUtil.toBigDecimal(10.10)) + //测试bigDecimal的avg单参 + Opt bigDecimalAvgOneParam = EasyStream.of(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10) + .map(NumberUtil::toBigDecimal) .avg(Function.identity()); Assert.assertEquals(NumberUtil.toBigDecimal(5.96), bigDecimalAvgOneParam.get()); - //测试bigDecimal的avg单参 - Opt bigDecimalAvgTwoParam = EasyStream.of(NumberUtil.toBigDecimal(1.1), - NumberUtil.toBigDecimal(2.2), NumberUtil.toBigDecimal(3.3), NumberUtil.toBigDecimal(4.4), - NumberUtil.toBigDecimal(5.5), NumberUtil.toBigDecimal(6.6), NumberUtil.toBigDecimal(7.7), - NumberUtil.toBigDecimal(8.8), NumberUtil.toBigDecimal(9.9), NumberUtil.toBigDecimal(10.10)) + //测试bigDecimal的avg双参 + Opt bigDecimalAvgTwoParam = EasyStream.of(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10) + .map(NumberUtil::toBigDecimal) .avg(Function.identity(), 2); Assert.assertEquals(NumberUtil.toBigDecimal(5.96), bigDecimalAvgTwoParam.get()); From 0b72ada03f64184956ff9633a1f25c20c974bd44 Mon Sep 17 00:00:00 2001 From: LoveHuahua Date: Tue, 28 Feb 2023 14:08:12 +0800 Subject: [PATCH 10/10] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/stream/EasyStream.java | 109 ++++++++++-------- 1 file changed, 58 insertions(+), 51 deletions(-) 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 84735a6a9..af022870d 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 @@ -5,14 +5,20 @@ import cn.hutool.core.lang.Opt; import cn.hutool.core.math.NumberUtil; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ObjUtil; - import java.math.BigDecimal; import java.math.RoundingMode; import java.util.List; import java.util.Objects; import java.util.OptionalDouble; import java.util.Spliterator; -import java.util.function.*; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.function.Supplier; +import java.util.function.ToDoubleFunction; +import java.util.function.ToIntFunction; +import java.util.function.ToLongFunction; +import java.util.function.UnaryOperator; import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.stream.StreamSupport; @@ -206,11 +212,11 @@ public class EasyStream extends AbstractEnhancedWrappedStream 元素类型 - * @param s 用来生成元素的 {@link Supplier} + * @param s 用来生成元素的 {@link Supplier} * @return 无限串行无序流 */ public static EasyStream generate(final Supplier s) { @@ -275,53 +281,54 @@ public class EasyStream extends AbstractEnhancedWrappedStream mapper) { + public int sum(final ToIntFunction mapper) { return stream.mapToInt(mapper).sum(); } /** - * 计算long类型总和 + * 计算long类型的总和 * - * @param mapper 映射 - * @return long + * @param mapper 将对象转换为long的 {@link Function} + * @return long 总和 */ - public long sum(ToLongFunction mapper) { + public long sum(final ToLongFunction mapper) { return stream.mapToLong(mapper).sum(); } /** - * 计算double总和 + * 计算double类型的总和 * - * @param mapper 映射器 - * @return double + * @param mapper 将对象转换为double的 {@link Function} + * @return double 总和 */ - public double sum(ToDoubleFunction mapper) { + public double sum(final ToDoubleFunction mapper) { return stream.mapToDouble(mapper).sum(); } + /** - * 计算number的总和 + * 计算 {@link Number} 类型的总和 * - * @param mapper 映射 - * @param 映射后的类型 - * @return {@link BigDecimal} + * @param 数字 + * @param mapper 将对象转换为{@link Number} 的 {@link Function} + * @return {@link BigDecimal} , 如果流为空, 返回 {@link BigDecimal#ZERO} */ public BigDecimal sum(final Function mapper) { - return stream.map(mapper).reduce(BigDecimal.ZERO, NumberUtil::add,NumberUtil::add); + return stream.map(mapper).reduce(BigDecimal.ZERO, NumberUtil::add, NumberUtil::add); } /** - * 计算bigDecimal平均值 并以四舍五入的方式保留2位精度 + * 计算 {@link BigDecimal} 类型的平均值 并以四舍五入的方式保留2位精度 * - * @param mapper 映射 - * @return 计算结果 如果元素的长度为0 那么会返回为空的opt + * @param mapper 将对象转换为{@link BigDecimal}的 {@link Function} + * @return 计算后的平均值 如果流的长度为0, 返回 {@link Opt#empty()} */ public Opt avg(final Function mapper) { return avg(mapper, 2); @@ -329,53 +336,53 @@ public class EasyStream extends AbstractEnhancedWrappedStream avg(final Function mapper, int scale) { + public Opt avg(final Function mapper, final int scale) { return avg(mapper, scale, RoundingMode.HALF_UP); } /** - * 计算bigDecimal平均值 + * 计算 {@link BigDecimal} 类型的平均值 * - * @param mapper 映射 - * @param scale 精度 + * @param mapper 将对象转换为{@link BigDecimal} 的 {@link Function} + * @param scale 保留精度 * @param roundingMode 舍入模式 - * @return 计算结果 如果元素的长度为0 那么会返回为空的opt + * @return 计算后的平均值 如果元素的长度为0 那么会返回 {@link Opt#empty()} */ - public Opt avg(final Function mapper, int scale, RoundingMode roundingMode) { + public Opt avg(final Function mapper, final int scale, + final RoundingMode roundingMode) { //元素列表 List bigDecimalList = stream.map(mapper).collect(Collectors.toList()); if (CollUtil.isEmpty(bigDecimalList)) { - return Opt.ofNullable(null); + return Opt.empty(); } - - return Opt.of(EasyStream.of(bigDecimalList).reduce(BigDecimal.ZERO, BigDecimal::add) + return Opt.ofNullable(EasyStream.of(bigDecimalList).reduce(BigDecimal.ZERO, BigDecimal::add) .divide(NumberUtil.toBigDecimal(bigDecimalList.size()), scale, roundingMode)); } /** - * 计算int平均值 + * 计算int类型的平均值 * - * @param mapper 映射器 - * @return {@link OptionalDouble} + * @param mapper 将对象转换为int 的 {@link Function} + * @return {@link OptionalDouble} 如果流的长度为0 那么会返回{@link OptionalDouble#empty()} */ - public OptionalDouble avg(ToIntFunction mapper) { + public OptionalDouble avg(final ToIntFunction mapper) { return stream.mapToInt(mapper).average(); } /** - * 计算double平均值 + * 计算double类型的平均值 * - * @param mapper 映射 - * @return {@link OptionalDouble} + * @param mapper 将对象转换为double 的 {@link Function} + * @return {@link OptionalDouble} 如果流的长度为0 那么会返回{@link OptionalDouble#empty()} */ - public OptionalDouble avg(ToDoubleFunction mapper) { + public OptionalDouble avg(final ToDoubleFunction mapper) { return stream.mapToDouble(mapper).average(); } @@ -383,10 +390,10 @@ public class EasyStream extends AbstractEnhancedWrappedStream mapper) { + public OptionalDouble avg(final ToLongFunction mapper) { return stream.mapToLong(mapper).average(); } @@ -407,9 +414,9 @@ public class EasyStream extends AbstractEnhancedWrappedStream{@code - * accept(t) - * return this; - * } + * accept(t) + * return this; + * } */ default Builder add(final T t) { accept(t);