changeset 12799:1edfa4abd77a

8136686: Collectors.counting can use Collectors.summingLong to reduce boxing Reviewed-by: psandoz Contributed-by: Tagir Valeev <amaembo@gmail.com>
author psandoz
date Sat, 19 Sep 2015 15:26:34 +0200
parents d11f25ce3c54
children 9f8e3bf8376a
files src/java.base/share/classes/java/util/stream/Collectors.java test/java/util/stream/test/org/openjdk/tests/java/util/stream/CountTest.java
diffstat 2 files changed, 8 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/util/stream/Collectors.java	Fri Sep 18 14:20:39 2015 -0700
+++ b/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/test/java/util/stream/test/org/openjdk/tests/java/util/stream/CountTest.java	Fri Sep 18 14:20:39 2015 -0700
+++ b/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