我有一个班级:Question
它有id
和content
。
我的HashMap
Questions
为键,asnwers(Strings)
为值。
我尝试从地图中查找答案,使用Question
作为密钥。
然而它总是提供null 值,我不知道为什么,因为地图确实包含我的问题。
代码:
Log.i("info", "map:");
Log.i("info", questionAnswers.toString());
Log.i("info", "================================");
Log.i("info", "question we are looking for:");
Log.i("info", question.toString());
Log.i("info", "================================");
Log.i("info", "anwer is null: " + questionAnswers.get(question));
Log.i("info", "================================");
Iterator it = questionAnswers.entrySet().iterator();
//Iterating the map just to get information for test's sake
while (it.hasNext()) {
Map.Entry<Question, String> entry = (Map.Entry<Question, String>) it.next();
//Question in the map
Question questionInCont = entry.getKey();
//Testing the equals method
if (questionInCont.equals(question)) {
Log.i("info", "================================");
Log.i("info", "question we are looking for is equals to a question in the map");
Log.i("info", questionInCont.getQuestionId() + " == " + question.getQuestionId());
String answer = questionAnswers.get(question);
//Great, equals working but still no answer back, only null
Log.i("info", "anwer is still null: " + answer);
Log.i("info", "================================");
}
}
日志输出:
11-21 13:22:13.083: I/info(18350): map:
11-21 13:22:13.083: I/info(18350): {Question{questionId='9', content='What is your name?'}=John, Question{questionId='8', content='How old are you?'}=33}
11-21 13:22:13.083: I/info(18350): ================================
11-21 13:22:13.083: I/info(18350): question we are looking for:
11-21 13:22:13.084: I/info(18350): Question{questionId='8', content='How old are you?'}
11-21 13:22:13.084: I/info(18350): ================================
11-21 13:22:13.084: I/info(18350): anwer is null: null
11-21 13:22:13.084: I/info(18350): ================================
11-21 13:22:13.084: I/info(18350): ================================
11-21 13:22:13.084: I/info(18350): question we are looking for is equals to a question in the map
11-21 13:22:13.084: I/info(18350): 8 == 8
11-21 13:22:13.084: I/info(18350): anwer is still null: null
11-21 13:22:13.084: I/info(18350): ================================
E D I T:
感谢您的评论,我的问题类没有hashCode()
功能。
这是问题吗?
Question class:
public class Question implements Serializable {
String questionId;
String content;
public Question(String id, String content) {
this.questionId = id;
this.content = content;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Question) {
Question question = (Question) obj;
return questionId.equals(question.getQuestionId());
}
return false;
}
}
播种为什么我为什么得到null作为答案?
答案 0 :(得分:5)
您的主要问题是您只实施equals()
方法。严格禁止使用HashMap的值,因为hashCode()
始终必须与--jars /path/to/spark-csv.jar,/path/to/commons-csv.jar
一起实现。
HashMap中的每个条目(m1)和第二个条目(m2)必须满足以下条件: m1.equals(m2)表示m1.hashCode()== m2.hashCode()
答案 1 :(得分:1)
您必须覆盖在HashMap中用作键的类的hashCode
方法。
可能很简单:
@Override
public int hashCode() {
return questionId.hashCode();
}
答案 2 :(得分:0)
正如其他人所说,HashMap可能无法检索与您的密钥关联的条目,因为您的Question类缺少hashCode()函数的覆盖(请参阅此参考:https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode())。 / p>
我建议按如下方式实现该功能:
@Override
public int hashCode() {
return questionId.hashCode() + content.hashCode();
}
一般规则是If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
(来自上面的链接)。