我想遍历地图中的几个条目......
在wizard()
中,我将 4 个映射放入map
,然后将映射与两个输入一起发送cancer
并test
进行计算...
public int wizard() {
Map<String, String> map = new HashMap<String, String>();
//historical data of having cancer given test result...
map.put("+cancer", "+test");
map.put("-cancer", "+test");
map.put("-cancer", "-test");
map.put("+cancer", "+test");
String cancer = "+cancer";
String test = "+test";
//send historical data to be calculated...
return calculate(cancer, test, map);
}
在这里,calcuate()
遍历映射索引以查找与两个输入cancer
和的匹配test
,然后返回条件概率:
public int calculate(String cancer, String test, Map<String, String> map) {
int tests = 0;
int both = 0;
System.out.println("Cancer: " + cancer + "; Test: " + test);
for (int i = 0; i <= map.size(); i++) {
if (map.containsValue(test)) {
tests++;
if (map.containsValue(cancer)) {
both++;
}
}
}
System.out.println("{Cancer & Tests}: " + both + "; Tests: " + tests);
return both/tests;
}
输出:
Cancer: +cancer; Test: +test
{Cancer & Tests}: 0; {Tests}: 3
P(Cancer|Test): 0
你可以看到它both++
没有增加(又名: {Cancer & Tests}
: 不应该0
),因此P(Cancer|Test)
没有给出正确的答案。
为什么是这样?我是否在地图上错误地迭代?