我已經實現了我的多鍵類如下: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