8136686: Collectors.counting can use Collectors.summingLong to reduce boxing
authorpsandoz
Sat, 19 Sep 2015 15:26:34 +0200
changeset 32755 6e8998981fbd
parent 32712 f61a63b7d1e5
child 32756 acd1d25bbc77
8136686: Collectors.counting can use Collectors.summingLong to reduce boxing Reviewed-by: psandoz Contributed-by: Tagir Valeev <amaembo@gmail.com>
jdk/src/java.base/share/classes/java/util/stream/Collectors.java
jdk/test/java/util/stream/test/org/openjdk/tests/java/util/stream/CountTest.java
--- a/jdk/src/java.base/share/classes/java/util/stream/Collectors.java	Wed Jul 05 20:50:41 2017 +0200
+++ b/jdk/src/java.base/share/classes/java/util/stream/Collectors.java	Sat Sep 19 15:26:34 2015 +0200
@@ -504,7 +504,7 @@
      */
     public static <T> Collector<T, ?, Long>
     counting() {
-        return reducing(0L, e -> 1L, Long::sum);
+        return summingLong(e -> 1L);
     }
 
     /**
--- a/jdk/test/java/util/stream/test/org/openjdk/tests/java/util/stream/CountTest.java	Wed Jul 05 20:50:41 2017 +0200
+++ b/jdk/test/java/util/stream/test/org/openjdk/tests/java/util/stream/CountTest.java	Sat Sep 19 15:26:34 2015 +0200
@@ -31,6 +31,7 @@
 
 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 @@
                 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