Unique closure on more than one field. « Intelligrape Groovy & Grails Blogs

Unique closure on more than one field.

Posted by Hitesh Bhatia

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

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

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

_________________________________
Hitesh Bhatia
Mail
LinkedIn,Facebook,Twitter
_________________________________

  • Share/Bookmark
This entry was posted on August 2nd, 2010 at 6:00 pm and is filed under Grails, Groovy . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses to “Unique closure on more than one field.”

  1. Subrata Banerjee says:

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

    Regards,
    Subrata

  2. hitesh says:

    Hi Subrata
    yes ..that would have been good alternative ..
    Thnx

Leave a Reply