drago
drago

Reputation: 1207

Groovy subcollection

What is the Groovy way to get collection items without items in subcollection. For example:

collection: [1,2,3,4,5,6]

subcollection: [1,5,6]

the result should be: [2,3,4]

EDIT: It looks I'm doing something wrong. This is part of my code:

    def report = Report.get(params.report.id)
    def user = User.get(params.user.id)
    List<User> availableUsers = []
    availableUsers = User.findAllByCompany(company))

    List<User> addedUsers = []
    addedUsers = (List<User>) session["addedUsers"] ?: []
    addedUsers << user
    session["addedUsers"] = null
    session["addedUsers"] = addedUsers

    availableUsers = availableUsers - addedUsers        

This code is only removing last user in addedUsers list.

availableUsers: [John, Jack, Jim]

addedUsers: [John, Jack]

availableUsers - addedUsers: [John, Jim]

Every time only the last item in addedUsers gets removed. I'm guessing I'm missing something obvious but I cant find it.

Upvotes: 0

Views: 221

Answers (1)

tim_yates
tim_yates

Reputation: 171144

Did you try the obvious:

result = [ 1, 2, 3, 4, 5, 6 ] - [ 1, 5, 6 ]

Because that is what works...

Edit

Storing domain object in the session will result in the object being different between hibernate transactions, better to store the id in the session and get the Users fresh each time (or write this functionality into the domain if it needs persisting), something like:

addedUsers = session["addedUsers"].collect { User.get( it.id ) } ?: []

Upvotes: 4

Related Questions