Groovy: Iterating a list

2019-03-06 10:56发布

I was trying to iterate a list and after googling, I found one solution like below:

value([0,1,2,3,4,5,6].sort{new Random()}?.take(1)[0])

I did not understand this part: sort{new Random()}

Can someone explain this? And which class the take method is belonging to?

1条回答
萌系小妹纸
2楼-- · 2019-03-06 11:44

sort receives a closure to determine the order that you wish. In this example, it makes a random sort.

take comes from Iterable and returns the first n elements.

查看更多
登录 后发表回答