Safest option to replace hashtable?
I have some old 3rd party Java code I am converting to Java 6. It contains HashTable
instances, which are flagged as obsolete collections. What should I开发者_Go百科 replace them with? What is the safest option?
Although not marked as deprecated in the API doc, HashTable
is obsolete, you could consider use HashMap
.
For the differences, you could check this post.
The main differences (the two that the doc said) are:
HashMap
is unsynchronized;HashMap
can containnull
s.
So beware of these two when you swap in HashMap
for HashTable
.
java.util.HashMap
has very similar semantics, however it is not synchronized. If you depend on the synchronized behavior, you can use java.util.concurrent.ConcurrentHashMap
精彩评论