Groovy : Difference between findAll() and collect() methods
findAll()
This method will find all the items in the groovy collection that matches the IDENTITY closure.
collect()
This method iterates through a collection and transforms each entry element to a new element using IDENTITY closure as a transformer. It returns a new collection of elements copied from the original collection.
Difference between finalAll() and collect() methods
findAll() | collect() |
---|---|
findAll() will return the collection or subset of collection that satisfy the Identity closure or a condition | collect() will return a new collection or sub set of collection. |
findAll() will not modify the entry elements of the collection | collect() will modify the elements by Identity closure. |
findAll() will return the elements that matches the condition or Identity closure. | collect() will return all the elements of the collection based on the transformation specified by the closure. |
def list = [1,2,3,4,5];
println list.findAll{it==3} // [3]
println list.findAll{it>3} // [4, 5]
println list.findAll{it*2} // [1, 2, 3, 4, 5]
println list.collect{it%2==0} // [false, true, false, true, false]
println list.collect{it==2} // [false, true, false, false, false]
println list.collect{it*2} // [2, 4, 6, 8, 10]
No comments: