I have a list of Parcel objects and am trying to write a closure that picks the parcel object with highest count:

class Parcel { 
int id, 
int count,
String sender,
String recipient 
Parcel(int _id, int _count) {
 id = _id
 count = _count 

def parcels = [new parcel(1,5), new parcel(2,1), new parcel(3,3), new parcel(4,2), new parcel(5,4) ]

I have tried the below code which works fine:


Is there a better way to pick the parcel with the highest count?


You could obviously try parcels.max{it.count}.

max and other useful operations on Groovy Collections are explained in the following article: https://groovy.codeplex.com/wikipage?title=Collections


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.