I have a List of "SomeClass" which may consist of a lot of functionally equal objects, and I need to group these. The entries with identical getName(), getInputDate(), getOutputDate() and getValue() should be grouped together. I came across Multimap which seems to be what i need, but I can't figure out an elegant way how to group these.
Currently I have used them all as the key to the Multimap, but I don't think this is the best solution..
private ImmutableListMultimap<String, SomeClass> getGrouped() {
return Multimaps.index
(someClassList, new Function<SomeClass, String>() {
public String apply(SomeClass someClass) {
return someClass.getName() + someClass.getInputDate() + someClass
.getOutputDate() + someClass.getValue();
}
});
}
The problem with your code is the key. Collapsing value into a string in order to obtain a key is not a good idea. I would create a key object and use the rest of your code:
update your code to something like