Unique closure on more than one field.

02 / Aug / 2010 by Hitesh Bhatia 2 comments

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]}
FOUND THIS USEFUL? SHARE IT

comments (2)

  1. Subrata Banerjee

    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 *