如何根据多个条件索引多图?
How to index a multimap based on several criteria?
我有一个 "SomeClass" 的列表,它可能包含许多功能相同的对象,我需要将它们分组。具有相同 getName()、getInputDate()、getOutputDate() 和 getValue() 的条目应分组在一起。我遇到了 Multimap,这似乎是我需要的,但我想不出一种优雅的方式来对这些进行分组。
目前我已经把它们都用作Multimap的关键,但我认为这不是最好的解决方案..
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();
}
});
}
你的代码问题是关键。将值折叠成字符串以获得键不是一个好主意。我会创建一个关键对象并使用您的其余代码:
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 that = (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;
}
}
将您的代码更新为
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());
}
});
}
我有一个 "SomeClass" 的列表,它可能包含许多功能相同的对象,我需要将它们分组。具有相同 getName()、getInputDate()、getOutputDate() 和 getValue() 的条目应分组在一起。我遇到了 Multimap,这似乎是我需要的,但我想不出一种优雅的方式来对这些进行分组。
目前我已经把它们都用作Multimap的关键,但我认为这不是最好的解决方案..
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();
}
});
}
你的代码问题是关键。将值折叠成字符串以获得键不是一个好主意。我会创建一个关键对象并使用您的其余代码:
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 that = (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;
}
}
将您的代码更新为
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());
}
});
}