.collect() -> _.map()
One convenient and powerful collection method in Groovy is .collect(), which we can use a function to transform all elements in a collection. Lo-Dash has a similar function _.map():
Note: Lo-Dash sometimes has additional methods that are effectively aliases of a main method, and _.map() is one of these methods that has an alias. You can also use the _.collect() alias instead of you prefer.
.find() -> _.find()
With find() in Groovy and in Lo-Dash we can find the first element in a collection that matches a given matcher function. For example, if we wanted to find the first element in a list that was an even number:
And if instead of finding the first element that matches the function you want to find the last element that matches, Lo-Dash has _.findLast() to do just that.
.findAll() -> _.filter()
Groovy’s .findAll() and Lo-Dash’s _.filter() are similar to find(), but instead of only returning the first element in a collection that matches a function, these methods will return all elements that match:
.each() -> _.forEach()
Groovy also has a handy way to run a function on each element in a collection with an each() loop, and Lo-Dash has equivalent capabilities with its _.forEach() function:
.sort() -> _.sortBy()
Similar to Groovy’s sort() method, we have a couple different options for how we want to sort our collecion. We can sort collections of simple items such as numbers by just passing the collection as a single parameter:
Or if we have a list of objects that share a common field, we can sort by that field:
And if we need the most flexibility, we can use a function to calculate the sort value:
.max() -> _.max()
.min() -> _.min()
Want to find the maximum or minimum number value of a list or array? Groovy has handy .max() and .min() method that you can call on the collection, and Lo-Dash has the same.