Reputation: 199
I need dual key concurrent hashmap.
My first try is just using java.util.concurrent.ConcurrentHashMap. Like this
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
map.put("key1" + "|" +"key2", "value");
String vaule = map.get("key1" + "|" +"key2");
but I think this is ugly.
My Second try is using Object as Key. Like this
@Data
public class DualKey {
private final String key1;
private final String key2;
}
map.put(new DualKey("key1", "key2"), "value");
String vaule = map.get(new DualKey("key1", "key2"));
Last try is create DualkeyConcurrentHashMap. I just need put, get, containsKey.
public class DualkeyConcurrentHashMap<K1, K2, V> {
private final ConcurrentHashMap<K1, ConcurrentHashMap<K2, V>> map
= new ConcurrentHashMap<>();
public V put(K1 key1, K2 key2, V value) {
ConcurrentHashMap<K2, V> subMap
= map.computeIfAbsent(key1, k -> new ConcurrentHashMap<>());
return subMap.put(key2, value);
}
public V get(K1 key1, K2 key2) {
ConcurrentHashMap<K2, V> subMap = map.get(key1);
return null == subMap ? null : subMap.get(key2);
}
public boolean containsKey(K1 key1, K2 key2) {
return null != get(key1, key2);
}
}
Is it better and perfectly thread safe? (I can't decide all method need synchronized.)
Is there another recommended way?
Upvotes: 2
Views: 1906
Reputation: 24324
All options are thread-safe, which is guaranteed by ConcurrentHashMap
. Important fact to note is:
However, even though all operations are thread-safe, retrieval operations do not entail locking, and there is not any support for locking the entire table in a way that prevents all access. This class is fully interoperable with Hashtable in programs that rely on its thread safety but not on its synchronization details.
The natural way to implement a dual key map would be to provide an object, so I would go with the second one, only that I would make DualKey
generic.
The first one couples implementation and design (string1 "|" + string1
key format) and does not allow you to change types used as keys easily.
The third one uses much more instances of ConcurrentHashMap
than needed.
Upvotes: 3