diff --git a/jdk/src/java.base/share/classes/java/util/stream/Collectors.java b/jdk/src/java.base/share/classes/java/util/stream/Collectors.java index 8ac48d8f73a..c3be0d97eb8 100644 --- a/jdk/src/java.base/share/classes/java/util/stream/Collectors.java +++ b/jdk/src/java.base/share/classes/java/util/stream/Collectors.java @@ -504,7 +504,7 @@ public final class Collectors { */ public static Collector counting() { - return reducing(0L, e -> 1L, Long::sum); + return summingLong(e -> 1L); } /** diff --git a/jdk/test/java/util/stream/test/org/openjdk/tests/java/util/stream/CountTest.java b/jdk/test/java/util/stream/test/org/openjdk/tests/java/util/stream/CountTest.java index 41f6ecf00b9..f626152e0f1 100644 --- a/jdk/test/java/util/stream/test/org/openjdk/tests/java/util/stream/CountTest.java +++ b/jdk/test/java/util/stream/test/org/openjdk/tests/java/util/stream/CountTest.java @@ -31,6 +31,7 @@ package org.openjdk.tests.java.util.stream; import java.util.HashSet; import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; import java.util.stream.DoubleStream; import java.util.stream.DoubleStreamTestDataProvider; import java.util.stream.IntStream; @@ -61,6 +62,12 @@ public class CountTest extends OpTestCase { expectedResult(expectedCount). exercise(); + // Test counting collector + withData(data). + terminal(s -> s, s -> s.collect(Collectors.counting())). + expectedResult(expectedCount). + exercise(); + // Test with stateful distinct op that is a barrier or lazy // depending if source is not already distinct and encounter order is // preserved or not