Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7164713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:07:14+00:00 2026-05-28T14:07:14+00:00

I am trying to count string values in a member of an object. I

  • 0

I am trying to count string values in a member of an object. I have tried three ways, but only one works. I am fine with the one that works, but I can’t understand why the others fail. Here’s the code:

void testCount() {
    TestObj a = new TestObj()
    TestObj b = new TestObj()
    TestObj c = new TestObj()
    a.s = "one"
    b.s = "two"
    c.s = "two"

    def list = [a, b, c]

    def count = 0
    list.each{
        if (it.s.equals("two"))
            count++
    }
    assertTrue("each test failed", count == 2)
    assertTrue("collectAll test failed", list.collectAll{ it.s.equals("two")}.size() == 2)
    assertTrue("count test failed", list.count{ it.s.equals("two")} == 2)        
}

I would expect the Closures passed to collectAll and count to do the same thing I’m doing in my each method. But in the case of collectAll it returns all 3 of the objects and in the case of count it always returns 0.

What am I missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:07:15+00:00Added an answer on May 28, 2026 at 2:07 pm

    collectAll is recursively going through your list, and returning a boolean (as that is what your closure returns for each element in the List)…

    So, you get [ false, true, true ], which has 3 elements…

    For count,

    list.count{ it.s == "two" }
    

    Returns 2 (as expected)

    btw: you can do it.s == 'two' in groovy.. no need for all the .equals( "two" )

    Edit… Example for count:

    class TestObj {
      String s
    }
    
    list = [ new TestObj( s:'one' ), new TestObj( s:'two' ), new TestObj( s:'two' ) ]
    
    println( list.count { it.s == 'two' } )
    

    Prints 2 for me…

    edit 2

    Found the cause (from comment below), count didn’t accept a closure as a parameter till 1.8 so you’ll be calling the object version which will tell you how many times an instance of the closure exists in the list (which is none, as it says)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to count all the values inside an array, I've tried using count()
I'm trying to count how many distinct value of FLOOR there is but I
I'm trying to count the number of records that have a null DateTime value.
I am trying to take in array which contains some integer values/string values from
Iam trying to pass dynamic argument values i.e username from request using spring ioc.But
I'm trying to parse a string into smaller ones, extracting some values and then
I'm trying to figure out how to grab only the values in between known
I'm trying to select distinct substring values of a field and count the number
I am trying to iterate only the first n values in my Map, is
Trying to count how many elements within the array are not equal to 0,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.