Details of the difference between the Hashtable class and the HashMap class in Java

  • 2020-04-01 04:37:05
  • OfStack

Hashtable class

Hashtable inherits the Map interface and implements a Hashtable for key-value mapping. Any non-null object can be used as a key or value.

Put (key, value) is used to add data, and get(key) is used to fetch data. The time cost of these two basic operations is constant.

Hashtable adjusts performance with two parameters, initial capacity and load factor. The default load factor 0.75 is generally a good time and space balance. Increasing the load factor saves space but increases the search time accordingly, which can affect operations like get and put.
A simple example of using Hashtable is to put 1, 2, and 3 into the Hashtable with keys "one", "two", and "three" :


Hashtable numbers = new Hashtable();
numbers.put("one", new Integer(1));
numbers.put("two", new Integer(2));
numbers.put("three", new Integer(3));

To extract a number, such as 2, use the corresponding key:


Integer n = (Integer)numbers.get("two");
System.out.println("two = " + n);

Since the object as key will determine the location of its corresponding value by calculating its hash function, any object as key must implement the hashCode and equals methods. The equals and hashCode methods inherited from the root class Object, if you use a custom class as the key, be very careful, as defined by the hash function, if two objects are the same, namely obj1) equals (obj2) = true, they must be the same hashCode, but if two objects are different, the hashCode they may not different, if two different objects of the same hashCode, this phenomenon is called conflict, conflict can lead to hash operation time cost increase, So the hashCode() method, as defined as possible, will speed up the operation of the hash table.

If the same object has a different hashCode, the operation on the hash table can yield unexpected results (the expected get method returns null). To avoid this problem, just remember to copy both equals and hashCode methods, not just one.
Hashtable is synchronous.

A HashMap class

A HashMap is similar to a Hashtable, except that a HashMap is asynchronous and allows for null, that is, null value and null key. But the (values() method returns a Collection when a HashMap is treated as a Collection, and its iterator operation time is proportional to the capacity of the HashMap. Therefore, do not set the initialization capacity of the HashMap too high or the load factor too low if the performance of the iteration operation is important.

conclusion

The methods of Hashtable are synchronous, and the HashMap is asynchronous, so you synchronize the HashMap manually in multi-threaded situations, just like Vector and ArrayList. Hashtable does not allow for null values (neither key nor value), and HashMap does allow for null values (both key and value). Hashtable has one more elements method for traversal than HashMap. Hashtable USES Enumeration, and HashMap USES Iterator. Hashtable directly USES the hashCode of the object, while HashMap recalculates the hash value and modulates it with and instead. The default size of the hash array in Hashtable is 11, and the increment is old*2+1. The default size of the hash array in a HashMap is 16, and it must be an exponent of 2.

Related articles: