If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Friday, February 19, 2010

Grouping objects from Java Collection

I had the need to group objects from a java collection on multiple occasions but a google search for easy ways to accomplish this was unsuccessful. For my last project i wrote some generic classes which enable me to group objects from any java collection based on a specified getter method. I also want them sorted based on the grouping key so the return type of the getter method needs to be a comparable.

So now we can get groups based on reflection with just a few lines of code:


  1. This comment has been removed by the author.

  2. Thank you for your contribution.....

    I have used this code ( modified a bit as I am group for a union of 3 fields instead of one ) and solved my need.

  3. To use a Class with different attributes as the grouping key, it's also needed you implement the equals method in the key class.