# Is there a way to do a collectEntries() on a FileTree?

**URL:** <https://discuss.gradle.org/t/is-there-a-way-to-do-a-collectentries-on-a-filetree/4347>\
**Category:** Old Forum Archive\
**Created:** [November 16, 2011, 5:54am UTC](https://discuss.gradle.org/t/is-there-a-way-to-do-a-collectentries-on-a-filetree/4347 "2011-11-16T05:54:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shorn](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@Shorn](https://discuss.gradle.org/u/Shorn)\
**Post date:** [November 16, 2011, 5:54am UTC](https://discuss.gradle.org/t/is-there-a-way-to-do-a-collectentries-on-a-filetree/4347/1 "2011-11-16T05:54:00Z")

</div>

At the moment, I’m doing this:

```gradle
def entries = []
myFileTree.each{ entries << it }
return entries.collectEntries([:]){ [it, myFileMappingFunction(it)] }

```

It feels wrong to be doing that intermediate call to “each(Closure)”.

I’d like to do this, or something like it:

```gradle
myFileTree.collectEntries([:]){ [it, myFileMappingFunction(it)] }

```

Am I missing something?

Where is the FileTree.each() defined, I couldn’t find it in the doco - I only know it exists because of the user guide.

---

<div class="post-metadata">

**Author:** ![luke\_daley](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/luke_daley/32/9890_2.png) [@luke\_daley](https://discuss.gradle.org/u/luke_daley)\
**Post date:** [November 16, 2011, 9:34am UTC](https://discuss.gradle.org/t/is-there-a-way-to-do-a-collectentries-on-a-filetree/4347/2 "2011-11-16T09:34:00Z")

</div>

Hi Shorn,

You’d have to do:

myFileTree.files.collectEntries([:]){ [it, myFileMappingFunction(it)] }

You’ll notice that [FileTree](http://gradle.org/current/docs/javadoc/org/gradle/api/file/FileTree.html) has a ‘getFiles()’ method that you can use to get a real collection that has all the Groovy methods.

The ‘each()’ method works out of the box because ‘FileTree’ implements ‘Iterable’, for which Groovy adds the ‘each()’ method. You could argue that ‘collectEntries()’ should be available to all 'Iterable’s, but that’s an argument for the Groovy language.

---

<div class="post-metadata">

**Author:** ![Shorn](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@Shorn](https://discuss.gradle.org/u/Shorn)\
**Post date:** [November 16, 2011, 9:36am UTC](https://discuss.gradle.org/t/is-there-a-way-to-do-a-collectentries-on-a-filetree/4347/3 "2011-11-16T09:36:00Z")

</div>

Ah, cool - that’ll do nicely.

Thanks.
