Tuesday, April 19, 2011

Commentary - Newcastle United v Manchester United

First Half:
----------
Tim Krul with an awesome reflex save off the Chicharito effort. Judged the perfect time to dive for the save.

Evra, apart from the lob that put Rooney through, has given the ball away every time.

Chicharito already with a couple of great shots on goal. Rooney and Nani looking potent, everyone else apart from Vidic and Smalling looking jittery still.

Newcastle playing some nice expansive football.

Giggs hasn't done much apart from bundling players over. Early days in the game yet . Come on you legend!

Jose Enrique looks like a player. Gets himself out of tough stops by some nice passes.

United finally with some measure of control in the Newcastle half.

Liking McManaman's commentary. On Newcastle's counter attack, United had 7 in the box compared to Newcastle's one for the Ameobi cross.

O'Shea dummy. Whoa. Nice unexpected play.

Sir Alex Ferguson looking on with a modern headset plugged into his ear. For once he has ditched the age-old landline phone.

What a useless booking for Michael Carrick. McManaman is right, the yellow card is more for the accumulated fouls from Man Utd, and a nothing foul on its own.

What a cross field pass from Rooney. Hernandez denied yet again!

End of half.


Sunday, April 10, 2011

Java stores a reference to the value when you put in hashmap, not a copy! #chilldownthespine

Literally shocked. Alright, this kind of feeling is not something I have experienced while working with Java before. Mostly it has been frustration leading me to take a long long break before returning to the code only to find that I was just plain silly. But this is different.

The offending piece of code: (simplified)

Double[] arr = new Double[N];

for (String key : sampleHashMap.keySet()) {
for (int i = 0; i < N; i++) {
arr[i] = get_some_random_value();
}
myHashMap.put(key, arr);
}

When we get to the line for updating arr[i], believe it or not, all values of arr already in myHashMap hashmap get updated!
There is just one double array arr. Every iteration, it gets updated with new values, and somehow, these values are reflected in the myHashMap for all keys which are already stored in there. It is as if all the value mappings in myHashMap, still point to the same address where tfIdfForAllDocs is written.

To solve this, I made sure I am creating a new double array every iteration.


for (String key : sampleHashMap.keySet()) {
   Double[] arr = new Double[N];  
for (int i = 0; i < N; i++) {
arr[i] = get_some_random_value();
}
myHashMap.put(key, arr);
}



When you do put operation on hashmap, the value that you are giving to the function, a copy of the value is NOT getting stored in the hashmap. Just the reference of the original value!