Q:

How to represent map with Groovy collectMaps

I have a Java code that looks like below code:
 for(MyClass myclassObject: input.classes()) {
        if(myclassObject.getName().equals("Tom")) {
            outputMap.put("output", myclassObject.getAge())
        }
    }

How do I efficiently write this with Groovy collectmap? 
I can do
input.classes().collectEntries["output":it.getAge()] But how do I include the if condition on it?

A:

you could use findAll to keep only items according to condition 
and after then apply collectEntries to transform items found 
@groovy.transform.ToString
class MyClass{
    int age
    String name
}

def classes = [
    new MyClass(age:11, name:'Tom'),
    new MyClass(age:12, name:'Jerry'),
]

classes.findAll{it.getName()=='Tom'}.collectEntries{ [output:it.getAge()] }

