If map is defined as
private Map<Integer, Integer> map = new HashMap<Integer, Integer>();
Updating the key works fine as shown below
public void increaseCountInFile(Integer hashCode) {
if (mapContains(hashCode)) {
increaseCount(hashCode);
} else {
map.put(hashCode, 1);
}
}
private void increaseCount(Integer fileHashCode) {
Integer key = map.get(fileHashCode);
map.remove(fileHashCode);
map.put(fileHashCode, ++key);
}
private boolean mapContains(Integer fileHashCode) {
return map.containsKey(fileHashCode);
}
However, i wonder, can the same be done without having to remove the element from the map? (map.remove(fileHashCode);)
++(map.get(fileHashCode)); Looks nice but Java does not like it
You don’t need
put()will automatically overwrite the entry. You can shorten yourincreaseCount()method like this:Honestly though, I think you’re adding too much abstraction in your code. It really should just be one method unless you have a very good reason otherwise: