What is Inject Method in Groovy?
问题 What does the inject method in Groovy actually do? I googled it, and have not found the exact answer. Can anyone specify its use with a simple example? 回答1: It applies an operation to a collection and keeps track of an intermediate value. Take this example: [1, 2, 3, 4].inject(0, { sum, value -> sum + value }) . This says use 0 as the initial value and apply the addition operation to the intermediate result and each element in sequence. Each application of the operation generates a new