java - How to index a multimap based on several criteria? -
i have list of "someclass" may consist of lot of functionally equal objects, , need group these. entries identical getname(), getinputdate(), getoutputdate() , getvalue() should grouped together. came across multimap seems need, can't figure out elegant way how group these.
currently have used them key multimap, don't think 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 code key. collapsing value string in order obtain key not idea. create key object , use rest of code:
public class persongroup{ private string name; private date input; private date output; private string value; public persongroup(string name, date input, date output, string value) { this.name = name; this.input = input; this.output = output; this.value = value; } @override public boolean equals(object o) { if (this == o) return true; if (o == null || getclass() != o.getclass()) return false; persongroup = (persongroup) o; if (input != null ? !input.equals(that.input) : that.input != null) return false; if (name != null ? !name.equals(that.name) : that.name != null) return false; if (output != null ? !output.equals(that.output) : that.output != null) return false; if (value != null ? !value.equals(that.value) : that.value != null) return false; return true; } @override public int hashcode() { int result = name != null ? name.hashcode() : 0; result = 31 * result + (input != null ? input.hashcode() : 0); result = 31 * result + (output != null ? output.hashcode() : 0); result = 31 * result + (value != null ? value.hashcode() : 0); return result; } } update code like
private immutablelistmultimap<persongroup, someclass> getgrouped() { return multimaps.index (someclasslist, new function<someclass, persongroup>() { public persongroup apply(someclass someclass) { return new persongroup(someclass.getname(), someclass.getinputdate(), someclass .getoutputdate(), someclass.getvalue()); } }); }
Comments
Post a Comment