[英]Compare two hashtable keys along with their values
我在JSP中使用哈希表。 我想比較兩個Hashtable值和鍵。 第一個哈希表包含問題編號(鍵)和用戶答案(值)。 第二個哈希表包含問題編號(鍵)和原始答案(值)。 我想先檢查問題編號是否匹配,然后再檢查相同問題編號的答案。 我想要最簡單的解決方案。我是學習者,而不是專家。 :)
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.