Search code examples
grailsgrails-orm

Find items from List based on matching property


I have a Groovy class:

class GlobalUsers {

    String name = ""
    String ID = ""
    ................
    ................
}

Now in another function:

List<GlobalUsers> guObjs = new List<GlobalUsers>();

Here I have few hundreds of objects of GlobalUsers in a List.

I want to find all objects of GlobalUsers where string name == "User_CUSTOM"

So basically the result will be another List:

List<GlobalUsers> guObjs = *name == "User_CUSTOM"*

Solution

  • You can use the findAll() method:

    def globalUsers = guObjs.findAll { it.name == "User_CUSTOM" }