I'm implementing the WeightedSlopeOne prediciton algorithm for recommender system and at some point in the code I need to have 2 2D maps, one Map<Integer, Map<Integer, Integer>>
and one Map<Integer, Map<Integer, Double>>
As you can understand accessing these and assigning values is a cumbersome procedure:
//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);
}
}
So what should I be using here instead of a Map<K1, Map<K2, V>>
, or if there is no better data structure available what is a better way to access and change the values of such a Map?