Java doesn't have reference, as a result its map implementations pretty ugly, and low efficient:

Map<String, Integer> m = new TreeMap<String, Integer>();
// ...
m.put("John", m.get("John") + 1); // i hate this

Reasons why I hate this:
1. Too long, too complicated for a so simple operation
2. Object boxing and unboxing
3. The binary tree is searched twice, one for set and one for get

In C++:

map<string, int> m;
// ....
m["John"]++; // cool

How to do the similar in Java:

.... First listen to me, eclipse is really a good IDE with many cool features. Amoung all these cool features I like CTRL+T most. I always discover something new when I press CTRL+T when some interface or abstract class is selected. And the class MutableInt was found when I pressed CTRL+T on Number.

So how do we do the similar thing in Java with MutableInt.

Map<String, MutableInt> m = new TreeMap<String, MutableInt>();
// ...
m.get("John").increment();
 


Comments




Leave a Reply