when I was coding, one question occurred to me, which is if the value part(Integer) in the HashMap is able to auto-increment in the following scenario?
JavaScript
x
Map<String, Integer> dictionary = new HashMap<String, Integer>();
dictionary.put("a",1);
dictionary.put("b",1);
Advertisement
Answer
Consider using an AtomicInteger
:
JavaScript
Map<Key, AtomicInteger> dictionary =
new HashMap<String, AtomicInteger>();
dictionary.get(key).incrementAndGet();
Also consider using a for
loop to simplify the code.