The difference between Hashtable class and HashMap class in Java is detailed _java

Source: Internet
Author: User
Tags hash

Hashtable class

Hashtable inherits the map interface and implements a hash table of key-value mappings. Any object that is Non-null (non-null) can be a key or value.

Adding data using put (key, value), fetching data using get (key), the time cost of these two basic operations is constant.

Hashtable adjusts performance by initial capacity and load factor two parameters. Typically, the default load factor 0.75 achieves a better balance of time and space. Increasing the load factor saves space but the corresponding lookup time increases, which can affect operations like get and put.
Using Hashtable for example, put 1,2,3 into Hashtable, their key is "one", "two", "three":

Hashtable numbers = new Hashtable ();
Numbers.put ("One", New Integer (1));
Numbers.put ("Two", New Integer (2));
Numbers.put ("Three", New Integer (3));

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

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

Because an object that is a key will determine the location of its corresponding value by calculating its hash function, any object that is a key must implement the Hashcode and Equals methods. The hashcode and Equals methods inherit from the root class object, and if you use a custom class as a key, be very careful, as defined by the hash function, if two objects are the same, that is, Obj1.equals (OBJ2) =true, Their hashcode must be the same, but if two objects are different, their hashcode is not necessarily different, and if the hashcode of two different objects is the same, this phenomenon is called conflict, and the conflict causes the time overhead of manipulating the hash table, so as to define the hashcode () method to speed up the operation of the hash table.

If the same object has a different hashcode, the operation of the hash table will have unexpected results (the expected GET method returns null), to avoid this problem, you need to keep in mind one: you have to copy both the Equals method and the Hashcode method, not just one.
The Hashtable is synchronized.

HashMap class

HashMap is similar to Hashtable, except that HashMap is unsynchronized and allows NULL, that is, null value and null key. However, when HashMap is treated as a collection (The values () method returns collection), its iterative child operation time cost is proportional to the capacity of the HashMap. Therefore, if the performance of an iterative operation is significant, do not set the HASHMAP initialization capacity too high, or the load factor too low.

Summarize

    The
    1. Hashtable method is synchronous, and HashMap is unsynchronized, so manual synchronization HashMap in multi-threaded situations is like vector and ArrayList. The
    2. Hashtable does not allow null values (both key and value), HashMap allow null values (both key and value can). The
    3. Hashtable is used to iterate over a elements method than HashMap. The
    4. Hashtable uses Enumeration,hashmap to use iterator. The
    5. hash value is used differently, Hashtable directly uses the object's hashcode, and hashmap the hash value, and modulo it in substitution. The default size of the hash array in the
    6. Hashtable is 11, and the increase is old*2+1. The default size of the hash array in HashMap is 16, and must be an exponent of 2.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.