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:
This comment has been removed by the author.
ReplyDeleteThank you for your contribution.....
ReplyDeleteI have used this code ( modified a bit as I am group for a union of 3 fields instead of one ) and solved my need.
To use a Class with different attributes as the grouping key, it's also needed you implement the equals method in the key class.
ReplyDelete