I'd suggest reading
http://groovy.codehaus.org/groovy-jdk/java/util/Collection.html
and
http://groovy.codehaus.org/Collections
A very naive way of doing this would be to use the collect method as follows
fileList.collect { file ->
//your condition
}.unique { file ->
//code that determines uniqueness
}
You could write as part of your condition in the collect closure to return false for duplicate items, then you wouldn't have to call unique()
[EDIT] Thinking about the problem you are faced with, you actually wouldn't want to use the above methods. Use the each method to iterate through your values. As I said, this is a naive way to complete your task, you may want to look into a good recursive method that piggy-backs off a sorting algorithm.
[EDIT] Merge 2 answers for same question (Efficiency design considerations)
This is a naive example using some of Justin Piper's code. Read through those APIs I posted as well as the List JDK. You will definately be able to make a more efficient method. This code should give you a good idea of how groovy works and what can be done with closures.
class File {
String name
int type
int revision
String toString() { "File(name: $name; type: $type; revision: $revision)" }
}
def files = [
new File(name: 'First Type2', type: 2, revision: 0),
new File(name: 'First Type0', type: 0, revision: 1),
new File(name: 'First Type1', type: 1, revision: 1),
new File(name: 'Second Type0', type: 0, revision: 0),
new File(name: 'Second Type1', type: 2, revision: 1),
new File(name: 'Second Type2', type: 1, revision: 1),
]
//This will hold the final set of files according to the logic in the next each()
def selectedFiles = [:]
files.each { file ->
//Overwrite the value associated with the key, which is the type depending on the logic - we only keep 1 of each type
if(selectedFiles[file.type]){
if(selectedFiles[file.type].revision < file.revision){
selectedFiles[file.type] = file
}
}
else{
//This type never existed, so just write the file as the value
selectedFiles[file.type] = file
}
}
selectedFiles.each { type, file ->
println(file)
}