Dasma
Dasma

Reputation: 1263

Is possible in groovy to remove a object from list through the object?

Is possible in groovy to remove a object from list through the object?

I know how to remove from the list, but can I remove it from the list with only know the reference of the certain object. (I don't want a null object in the list)

Probably it is not possible, but Groovy have surprises.

class Foo() {
    List<Boo> boos
}

class BoosHandler {
    void doSomethingWithBoo() {
        boos.each {
           analyse(it)
        }
    }

    void analyse(Boo boo) {
        if(boo.something == "wrong") {
           remove(boo) // Pseudo style for removing object boo from the list (Foo.boos)
        }
    }
}

Upvotes: 0

Views: 345

Answers (1)

tim_yates
tim_yates

Reputation: 171084

No, not possible.

I would also not do this, as in a multi-threaded environment it would be unpredictable...

You're better (as you probably know) doing:

List<Boo> filter(String notThis) {
    boos.findAll { it.something != notThis }
}

ie: return a new list, and don't change the original

Upvotes: 1

Related Questions