HashMap没有按键找到值 - 为什么?

时间:2016-11-21 12:32:14

标签: java android

我有一个班级:Question

它有idcontent

我的HashMap Questionsasnwers(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): ================================

所以你可以看到:

  1. 我的HashMap 包含问题我正在寻找。
  2. 确实有答案(价值)
  3. 当两个问题具有相同的ID时,等于方法的问题确实返回
  4. 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作为答案?

3 个答案:

答案 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.(来自上面的链接)。

相关问题