问题
What's the difference between Dictionary
and Hashtable
and how do I work with the Dictionary
class in Java?
回答1:
Dictionary
is an abstract base class of Hashtable
. Both are still in JDK for backwards compatibility with old code. We are expected to use HashMap
and other implementations of Map
interface introduced in Java 1.2.
回答2:
The javadoc for Dictionary has your answer.
The Dictionary class is the abstract parent of any class, such as Hashtable, which maps keys to values.
You don't work directly with Dictionary
, since it is an abstract
class.
Also note the following from the same documentation:
NOTE: This class is obsolete. New implementations should implement the Map interface, rather than extending this class.
回答3:
Dictionary
is an abstract class, superclass of Hashtable
.
You should not use Dictionary
as it is obsolete.
As for Hashtable, the advantage it had over other maps such as HashMap
was thread safety, but with the introduction of ConcurrentHashMap since Java 1.5, there is no real reason to use it any longer - see javadoc
As of the Java 2 platform v1.2, this class was retrofitted to implement the Map interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.
In summary: Don't use Dictionary
or Hashtable
, unless you really have to for compatibility reasons, use either HashMap
if you don't need thread safety, or ConcurrentHashMap
if your map is used in a concurrent environment.
回答4:
Hashtable
is an implementation of Dictionary
. You can't use Dictionary
directly because it's an abstract class.
But you shouldn't use either because they have been superceded by the Map
interface and the implementing classes, of which HashMap is the most popular.
回答5:
I've found a lecture on Principles of OOP that contains the answer you seek:
http://www.clear.rice.edu/comp202/04-fall/lectures/lec23/
EDIT:
Dictionary A major theme in computing is the theme of storage/retrieval/removal: store data somewhere so that it can later be retrieved and discarded if no longer needed, all of this in the most efficient manner. The abstraction of these computing activities is embodied in the notion of what is called a dictionary, expressed in Java as an interface as follows.
Hash Tables A hash table is a generalization of an ordinary array. When the number of keys actually stored is small relative to the total number of possible keys, hash tables become an effective alternative to directly addressing an array, since a hash table typically uses an array of size proportional to the number of keys actually stored. Instead of using the key as an array index directly, the array index is computed from the key. With hashing, an element with key k is stored in slot h(k); i.e., a hash function h is used to compute the slot from the key k. h maps the set U of keys into the slots of a hash table T[0..m-1]: h:U -> {0, 1, ..., m - 1}
回答6:
The Dictionary class is the abstract parent of any class, such as Hashtable, which maps keys to values. Every key and every value is an object. In any one Dictionary object, every key is associated with at most one value. Given a Dictionary and a key, the associated element can be looked up. Any non-null object can be used as a key and as a value.
回答7:
According to the javadocs for Dictionary:
NOTE: This class is obsolete. New implementations should implement the Map interface, rather than extending this class.
Hashtable is JDK 1.0 vintage, too. You should prefer the Map interface and its more modern implementations: HashMap and TreeMap.
来源:https://stackoverflow.com/questions/9769797/whats-the-difference-between-hashtable-and-dictionary