我有一个 Parcel 对象列表,正在尝试编写一个闭包来选择计数最高的 Parcel 对象
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) ]
我尝试了下面的代码,效果很好
parcels.sort{it.count}
parcels.reverse().first()
我想知道是否有更好的方法来挑选数量最多的包裹?
最佳答案
我想这个选项应该有效:
assert parcels.max { it.count }.count == 5
关于arraylist - Groovy 列表中计数最高的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27409072/