Do I have to return the object and then put a new one in ? Or can I just directly increment ?
Integer temp = myMap.get(key);
Do I have to return the object and then put a new one in ?
As long as you use the
Integer wrapper class yes, because it's immutable. You could use a mutable wrapper class instead, even one that has an
increment() method. However, you then lose the ability to use autoboxing and autounboxing on the values.