If you want to be thread safe with respect to counter initialization, you should use ConcurrentHashMap and always start and increment counters as follows:
themap.putIfAbsent("the name", new AtomicInteger(0)); themap.get("the name").incrementAndGet();
You can also make sure that you initialize all the counters used before the start, and just use any collection you like. The simple AtomicInteger[] -array is much faster, given that you know where to look, a HashTable can be a little faster than a HashMap .
If you know in advance which counters you have, you can also determine the java enum all the counter names and use EnumMap<YourCountersEnum, AtomicInteger> . This will likely result in search results being close to AtomicInteger[] -array searches.
source share