Remove list of files from another list of files by their name In groovy I have a list of files A and also another list of files B. I want to remove all the files from A that have a filename that also occurs in the list of files B. I wrote the following code:

List&lt;File&gt; removeByName(final List&lt;File&gt; files, final List&lt;File&gt; removalFiles) {
    final Set&lt;String&gt; removalNames = removalFiles.collect { it.name }.toSet()
    files.findAll { ! removalNames.contains(it.name) }
}


Unfortunately I cannot use the remove method in the Collection class because two files are not equal even if they have the same file name. Are there any methods that I am missing which could be used to make that code cleaner?
Groovy is weakly typed, so you could save a some cruft using def for local variables. Typing can give nice hints (great on an API), but it can also be ignored, leading to code lies and confusion. Final is also a pretty pointless keyword in Groovy.

There's also a shortcut to collecting properties: the spread operator *, which allows you to do removalFiles*.name. It's also more groovy to cast to a set with as Set.

If you really need to do an in-place change, what you want to do is collect all the file objects from the files collection that you want to remove in order to remove them. This way, you are removing using the exact same object:

void removeByNameInPlace(def files, def removalFiles) {
    def removalNames = removalFiles*.name as Set
    files.removeAll( files.findAll { removalNames.contains(it.name) } )
}


However this isn't very groovy and it's typical to return modified copies of Collections instead. This makes your code pretty close to ideal, functionally speaking. With some stylistic changes to your original code:

List&lt;File&gt; removeByName(List&lt;File&gt; files, List&lt;File&gt; removalFiles) {
    def removalNames = removalFiles*.name as Set
    files.findAll { ! removalNames.contains(it.name) }
}