Vano
Vano

Reputation: 1538

Groovy .each vs .collect

As part of beginners Groovy workshop, We've been iterating over the following list (fromJson.secrets):

[[floors:10, street:emaseS, url:http://plywoodpeople.com/wp-content/uploads/2012/03/kermit_the_frog.jpg], [floors:2, street:emaseS, url:http://36.media.tumblr.com/tumblr_lp9bg9Lh2x1r0h9bqo1_500.jpg], [floors:2, street:yawdaorB, url:https://montclairdispatch.com/wp-content/uploads/2013/07/broadway1.jpg], [floors:5, street:emaseS, url:AAA], [floors:2, street:yawdaorB, url:AAA], [floors:6, street:albmaR aL, url:AAA], [floors:1, street:teertS llaW, url:AAA], [floors:6, street:daoR yebbA, url:AAA], [floors:3, street:teertS llaW, url:AAA], [floors:4, street:dlirehstoR, url:AAA]]

The original plan was to use .collect, however it looks like using .each produced the same results (iterated over the list...).

The questions is, can someone help me to understand the difference between the methods in regard to my use case and in general

each:

reversed_streets = fromJson.secrets.each {
    it.street = it.street.reverse()
    it
}


collect:

reversed_streets = fromJson.secrets.collect {
    it.street = it.street.reverse()
    it
}

Upvotes: 18

Views: 18817

Answers (1)

cfrick
cfrick

Reputation: 37008

  • each is used for side effects (which is your example) and returns the original
  • collect is used to create something new (e.g. like an eager map in Java or other languages)

In your example: each returns the input to each. Your code there manipulates it.street in place. So you get back your original list, where each street got reversed. With the collect you create a new list with the manipulated items. So the apparent result is the same, but the difference is that you created a new container, but still your original has been tampered with.

Upvotes: 35

Related Questions