I am new to groovy Lists, and I have list as shown below:
Class File{
String name
int type
int revision
}
def fileList = [File1, File2, File3,....]
I want the fileList
to have the latest files
- It should not have the items of same type
- It should have the highest revision files means, if two or more files have the same type then the list must have the single file with the highest revision.
How do I do this in Groovy?
You can use
Collection#groupBy
to create a map with an entry for each unique file name. You can then useMap#collect
to iterate over the contents of this map and create the list you want. The map's values will be a list of instances ofFile
, soCollection#max
will let you search for the one with the highest revision number.I'm not sure what you meant when you said that the list should not have items of the same type. You'll notice that if there are two instances of
File
with the same name and revision number but different types, this solution picks one arbitrarily.