假设有两个HashMap,如下所示:
HashMap<String, Integer> h1 = [{"a":1}, {"b":2}, {"c":3}];
HashMap<String, Integer> h2 = [{"k": 1}, {"f": 4}, {"g":5}, {"a":10}]
乘法就像一个简单的矢量乘法,在这种情况下,它将返回1 * 10 + 2 * 0 + 3 * 0 = 10。
即,如果键相同,则仅将两个相应的值相乘。
结果->它应返回一个整数。
int result = 0;
for(String s : h1.keySet()){
if(h2.containsKey(s)){
result = result + h2.get(s) * h1.get(s);
}
}