Unique closure on more than one field.

1 min read
Share:

In one of my recent grails project, I had a requirement to get unique objects from a list based on more than one field. Now, this can be done by passing the list to closure unique.

I had a list of dates of a couple of years, and what I needed was a unique combination of month and year.

 
List  dates 
date.unique{[it.month,it.year]}

comments ( 2 )

  1. Hi Hitesh,
    That’s really helpful post. Unless we used to take TreeSet to avoid duplication.
    Thanks for this valuable concept.

    Regards,
    Subrata

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *