.findLast()
over .reverse.find()
SC-P1012The reverse method reverses all the ordering of the collection. Chaining it with find()
returns the first element that satisfies the given predicate.
However, the reverse operation here is inefficient as elements except the first-satisfying element are discarded.
It is recommended that you instead use .findLast()
as:
There is no reversing involved, and,
No resources are allocated for the intermediate buffer to hold the reversed order as the operation itself is made redundant.
nums.reverse.find(_ % 2 == 0)
nums.findLast(_ % 2 == 0)