Groovy: Iterating a list

2019-03-06 11:10发布

问题:

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:

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.