简体   繁体   English

比较两个哈希表键及其值

[英]Compare two hashtable keys along with their values

I am using Hashtables in JSP. 我在JSP中使用哈希表。 I want to compare two Hashtable values and keys. 我想比较两个Hashtable值和键。 First Hashtable contains question number(key) and user answer(value). 第一个哈希表包含问题编号(键)和用户答案(值)。 Second Hash Table contains question number (key) and original answer (value). 第二个哈希表包含问题编号(键)和原始答案(值)。 I want to check first if question number match then i want to check answer for same question number. 我想先检查问题编号是否匹配,然后再检查相同问题编号的答案。 I want simplest solution.I am a learner, not expert. 我想要最简单的解决方案。我是学习者,而不是专家。 :) :)

    public static void main(String[] args) {

        //Actual Answers
        Map<Integer,String> first = new Hashtable<Integer, String>();
        first.put(1,"A");
        first.put(2,"B");
        first.put(3,"C");

        //User Answers
        Map<Integer,String> second = new Hashtable<Integer, String>();
        second.put(1,"A");
        second.put(2,"A");
        second.put(3,"A");
        checkAnswers(first,second);
}

        public static void checkAnswers(Map<Integer,String> first, Map<Integer,String> second){ 
            int chkcount = 0;
            for(Map.Entry<Integer,String> entry: second.entrySet()){
                String actualAnswer = first.get(entry.getKey());
                String givenAnswer = entry.getValue();
                if(givenAnswer != null && actualAnswer != null && givenAnswer.equals(actualAnswer)){
                    chkcount = chkcount + 1;
                }
            }
            System.out.println(chkcount);

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM