groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] asfgit closed pull request #849: GROOVY-8945: Improve performance of DGM `groupBy` and `countBy`
Date Sat, 05 Jan 2019 13:52:26 GMT
asfgit closed pull request #849: GROOVY-8945: Improve performance of DGM `groupBy` and `countBy`
URL: https://github.com/apache/groovy/pull/849
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index f2a6b26ffa..db43fd276b 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -5883,20 +5883,23 @@ public static Map groupBy(Object[] self, List<Closure> closures)
{
      * @since 1.5.0
      */
     protected static <K, T> void groupAnswer(final Map<K, List<T>> answer,
T element, K value) {
-        if (answer.containsKey(value)) {
-            answer.get(value).add(element);
-        } else {
-            List<T> groupedElements = new ArrayList<T>();
-            groupedElements.add(element);
+        List<T> groupedElements = answer.get(value);
+
+        if (null == groupedElements) {
+            groupedElements = new ArrayList<T>();
             answer.put(value, groupedElements);
         }
+
+        groupedElements.add(element);
     }
 
     private static <T> void countAnswer(final Map<T, Integer> answer, T mappedKey)
{
-        if (!answer.containsKey(mappedKey)) {
-            answer.put(mappedKey, 0);
+        Integer current = answer.get(mappedKey);
+
+        if (null == current) {
+            current = 0;
         }
-        int current = answer.get(mappedKey);
+
         answer.put(mappedKey, current + 1);
     }
 
diff --git a/src/test/groovy/GroovyMethodsTest.groovy b/src/test/groovy/GroovyMethodsTest.groovy
index b23fd27723..dd31077236 100644
--- a/src/test/groovy/GroovyMethodsTest.groovy
+++ b/src/test/groovy/GroovyMethodsTest.groovy
@@ -786,6 +786,13 @@ class GroovyMethodsTest extends GroovyTestCase {
         assert result2['cc']['cd']['ce'] == [cc: 33]
     }
 
+    void testCountBy() {
+        def list = ['a', 'b', 'c', 'a']
+        def result = list.countBy { it }
+
+        assert [a: 2, b: 1, c: 1] == result
+    }
+
     def leftCol = ["2"]
     def rightCol = ["1", "2", "3"]
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message