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#groupByto create a map with an entry for each unique file name. You can then useMap#collectto 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#maxwill 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
Filewith the same name and revision number but different types, this solution picks one arbitrarily.