Should I use HashTable or HashMap as a data cache?

caching, hashmap, hashtable, java

Solution

Use `ConcurrentHashMap`

It provides high-performance concurrent implementation of the standard `Map` interface.

To provide high concurrency, the implementation manages its own synchronization internally. So, it is impossible to exclude concurrent activity from it, locking it will have no effect but to slow the program.

`ConcurrentHashMap` is optimized for retrieval operations, such as `get`. `ConcurrentHashMap` extends `Map` and adds several methods, including `putIfAbsent(key, value)`, which inserts a mapping for a key if none was present and returns the previous value associated with the key, or `null` if there was none. This makes it easy to implement thread-safe maps.

Besides offering excellent concurrency, ConcurrentHashMap is very fast. Unless you have a compelling reason to do otherwise, use `ConcurrentHashMap` in preference to `Collections.synchronizedMap` or `Hashtable`. Simply replacing old-style synchronized maps with concurrent maps can dramatically increase the performance of concurrent applications.

For more information I would suggest reading Effective Java.

Problem

I want to create a cache of data in memory, so that I do not have to query the database everytime. The dictionary will initially be empty, and it will be populated as data is requested from it. For this purpose, should the dictionary be a HashTable or a HashMap? Will ConcurrentHashMap or synchronizedMap be better? Please explain why X is better than Y for my purpose. I am trying to understand the differences between them. ``` //variable for locking purposes. private String LOCK = new String("LOCK"); //HashMap or HashTable? private HashMap<Long, Long> dictionary = new HashMap<Long, Long>(); public Long getValue(Long key) throws SQLException{ Long value = dictionary.get(key); if(value != null) return value ; synchronized (LOCK) { //Check again to make sure another thread hasn't populated the dictionary key = dictionary.get(key); if(key != null) return value; //Get value from database //store in map for future use dictionary.put(key, value); } return value; } ``` Since I want multiple threads to read data simultaneously, I am not synchronizing the entire method. However, inserting data into the dictionary should be synchronized. I will not be storing null values. P.S. Could someone explain the differences between them WITH EXAMPLES?

Original source