1.HashTable method is synchronous, HashMap is not synchronized, so in multi-threaded occasions to manually synchronize HashMap this difference is like vector and ArrayList. can use synchronized to achieve HashMap synchronization, but will add a lot of processing costs, efficiency hashmap to high
2.HashTable does not allow null values, neither key nor value can, HASHMAP allows null values, both key and value. HashMap allows the key value to be used only by a null value, because HASHMAP if the key value is the same, the new key, value will replace the old one.
3.HashTable has a contains (object value) feature and Containsvalue (object value) functionality. HashMap removed the Hashtable contains method, but added the Containsvalue () and ContainsKey () methods.
4.HashTable use Enumeration,hashmap with iterator. Hashtable is only a elements method more than HashMap.
- Enumeration em = Table.elements ();
- while (Em.hasmoreelements ()) {
- String obj = (string) em.nextelement ();
- System.out.println (obj);
- }
The default size of the hash array in 5.HashTable is 11, and the increment is old*2+1. The default size of the hash array in HashMap is 16, and must be a 2 index.
6. Using different hash values, Hashtable directly uses the object's hashcode.
Hashtable directly uses the object's Hashcode, which is the code:
- int hash = Key.hashcode ();
- int index = (hash & 0x7FFFFFFF)% Tab.length;
Instead, HashMap recalculates the hash value and uses and replaces the modulo:
- int hash = hash (k);
- int i = indexfor (hash, table.length);
- static int hash (Object x) {
- int h = X.hashcode ();
- H + = ~ (h << 9);
- H ^= (H >>> 14);
- H + = (h << 4);
- H ^= (H >>> 10);
- return h;
- }
- static int indexfor (int h, int length) {
- Return H & (length-1);
About Properties
Sometimes, you might want to use a hashtable to map the string of key to the string of value. The environment strings in DOS, WINDOWS, and Unix have some examples, such as the string PATH of key that is mapped to the string C:\WINDOWS of value; C:\WINDOWS\SYSTEM. Hashtables is a simple way to represent these, but Java provides another way.
The Java.util.Properties class is a subclass of Hashtable, designed for string keys and values. The use of the properties object is similar to the use of Hashtable, but the class adds two time-saving methods, which you should know.
The Store () method saves the contents of a Properties object to a file in a readable form. The Load () method is exactly the opposite, used to read the file and set the Properties object to contain keys and values.
Note that because properties extends the Hashtable, you can use the put () method of the superclass to add keys and values that are not string objects. This is not advisable. In addition, if you use the store () for a Properties object that does not contain a string object, store () will fail. As an alternative to put () and get (), you should use SetProperty () and GetProperty (), which use the string parameter.
The difference between HashMap and Hashtable