简体   繁体   中英

Compare two hashtable keys along with their values

I am using Hashtables in JSP. I want to compare two Hashtable values and keys. 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);

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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