我已经实现了我的多键类如下:HashMap上的多个键:它删除现有的值?
public class ProbabilityIndex {
private int trueLabel;
private int classifiedLabel;
private int classifierIndex;
public ProbabilityIndex(int trueLabel, int classifiedLabel, int classifierIndex) {
this.trueLabel = trueLabel;
this.classifiedLabel = classifiedLabel;
this.classifierIndex = classifierIndex;
}
@Override
public boolean equals(Object obj) {
if (!obj instanceof ProbabilityIndex)
return false;
if (obj == this)
return true;
ProbabilityIndex rhs = (ProbabilityIndex) obj;
return new EqualsBuilder().
append(trueLabel, rhs.trueLabel).
append(classifiedLabel, rhs.classifiedLabel).
append(classifierIndex, rhs.classifierIndex).
isEquals();
}
@Override
public int hashCode() {
int hashCode = new HashCodeBuilder(17, 31).
append(trueLabel).
append(classifiedLabel).
append(classifierIndex).
toHashCode();
return hashCode;
}
}
注意trueLabel
,classifiedLabel
和classifierIndex
均为0或1
然后,我用我的钥匙如下:
ProbabilityIndex key = new ProbabilityIndex(trueLabel, classifiedLabel, classifierIndex);
probabilities.put(key, new Double(value));
其中probabilities
声明如下:
HashMap<ProbabilityIndex, Double> probabilities;
但是,trueLabel
,classifiedLabel
和classifierIndex
的不同组合将该元组写入probabilities
中的相同位置,从而覆盖现有的元组。
我该如何解决这个问题?
最少测试用例:
HashMap<ProbabilityIndex, Double> map = new HashMap<ProbabilityIndex, Double>();
map.put(new ProbabilityIndex(0, 0, 0), new Double(0.1));
map.put(new ProbabilityIndex(0, 0, 1), new Double(0.2));
map.put(new ProbabilityIndex(0, 1, 0), new Double(0.1));
map.put(new ProbabilityIndex(0, 1, 1), new Double(0.2));
map.put(new ProbabilityIndex(1, 0, 0), new Double(0.1));
这将插入4元组,而不是5
你可以构造一个[minimal test-case](http://stackoverflow.com/help/mcve)来证明这一点吗? – 2014-09-27 17:14:45
什么是EqualsBuilder? – SJha 2014-09-27 17:15:28
@SJha:https://www.google.co.uk/search?q=equalsbuilder – 2014-09-27 17:17:14