public double CosSim(HashMap<String, Double> hashA,HashMap<String, Double> hashB,double sa,double sb) { double sum=0; HashMap<String, Double> tmp=null; if (hashA.size()>hashB.size()) { tmp=hashA; hashA=hashB; hashB=tmp; } java.util.Iterator<String> iterator = hashA.keySet().iterator(); while (iterator.hasNext()) { String word = iterator.next(); if(hashB.containsKey(word)) { sum=sum+hashA.get(word)*hashB.get(word); } } sum=sum/(sa*sb); return sum; }