我正在为推荐系统实现 WeightedSlopeOne 预测算法,并且在代码中的某个时刻,我需要有 2 个 2D 地图,一个Map<Integer, Map<Integer, Integer>>
和一个Map<Integer, Map<Integer, Double>>
正如您所理解的,访问这些和分配值是一个繁琐的过程:
//The following 20 lines are 1 line in Python. Sigh...
HashMap<Integer, Integer> freqsForItem1 = frequencies.get(curItemID);
//See if we have a value for curItemID
if (freqsForItem1 == null) {
freqsForItem1 = new HashMap<Integer, Integer>();
freqsForItem1.put(curItemID_2, 1);
frequencies.put(curItemID, freqsForItem1);
}
else {//See if we have a value for curItemID+curItemID_2
Integer freqForItem1Item2 = freqsForItem1.get(curItemID_2);
if (freqForItem1Item2 == null) {
//If we don't have a value for item1+item2 we just put 1
freqsForItem1.put(curItemID_2, 1);
}
else {//We already have a value for curItemID+curItemID_2
//So we just increment it
freqsForItem1.put(curItemID_2, freqForItem1Item2 + 1);
}
}
那么我应该在这里使用什么而不是 a Map<K1, Map<K2, V>>
,或者如果没有更好的数据结构可用,那么访问和更改此类 Map 的值的更好方法是什么?