HashMap Hashtable Difference

Source: Internet
Author: User
Tags rehash

Http://blog.csdn.net/java2000_net/archive/2008/06/05/2512510.aspx

Let's first look at the definition of 2 classes.

[Java]View Plain Copy
    1. public class Hashtable
    2. Extends Dictionary
    3. Implements Map, Cloneable, java.io.Serializable
[Java]View Plain Copy
    1. public class HashMap
    2. Extends Abstractmap
    3. Implements Map, Cloneable, Serializable

Visible hashtable inherit from Dictiionary and HashMap inherit from Abstractmap

The Put method of Hashtable is as follows

[Java]View Plain Copy
  1. Public synchronized v put (K key, V value) {//###### Note here 1
  2. Make sure the value was not null
  3. if (value = = null) {//###### NOTE here 2
  4. throw new NullPointerException ();
  5. }
  6. Makes sure the key is not already in the Hashtable.
  7. Entry tab[] = table;
  8. int hash = Key.hashcode (); ###### Note here 3
  9. int index = (hash & 0x7FFFFFFF)% Tab.length;
  10. for (Entry e = Tab[index]; E! = null; e = e.next) {
  11. if ((E.hash = = hash) && e.key.equals (key)) {
  12. V old = E.value;
  13. E.value = value;
  14. return old;
  15. }
  16. }
  17. modcount++;
  18. if (count >= threshold) {
  19. Rehash the table if the threshold is exceeded
  20. Rehash ();
  21. tab = table;
  22. Index = (hash & 0x7FFFFFFF)% Tab.length;
  23. }
  24. Creates the new entry.
  25. Entry e = Tab[index];
  26. Tab[index] = new Entry (hash, key, value, E);
  27. count++;
  28. return null;
  29. }

Note The 1 method is synchronous
Note the 2 method does not allow Value==null
Note that the 3 method calls the Hashcode method of key, and if key==null, throws a null pointer exception HashMap The Put method is as follows

[Java]View Plain Copy
  1. Public V put (K key, V value) {//###### Note here 1
  2. if (key = = null)//###### Note here 2
  3. return Putfornullkey (value);
  4. int hash = hash (Key.hashcode ());
  5. int i = indexfor (hash, table.length);
  6. for (Entry e = table[i]; E! = null; e = e.next) {
  7. Object K;
  8. if (E.hash = = Hash && (k = e.key) = = Key | | key.equals (k)) {
  9. V oldValue = E.value;
  10. E.value = value;
  11. E.recordaccess (this);
  12. return oldValue;
  13. }
  14. }
  15. modcount++;
  16. AddEntry (hash, key, value, I); ######, watch this.
  17. return null;
  18. }

Note The 1 method is non-synchronous
Note The 2 method allows key==null
Note the 3 method does not make any calls to value, so allow null

Add:
Hashtable has a contains method, easy to cause misunderstanding, so in hashmap inside has been removed
Of course, all 2 classes use the ContainsKey and Containsvalue methods.

HashMap Hashtable

Parent class Abstractmap Dictiionary

Whether to synchronize No is

K,v can null whether

HashMap is a lightweight implementation of Hashtable (non-thread-safe implementation), they all complete the map interface,

The main difference is that HASHMAP allows null (NULL) key values (key), which may be more efficient than Hashtable due to non-thread safety.

HashMap allows NULL to be used as a entry key or value, and Hashtable is not allowed.

HashMap hashtable contains method removed, changed to Containsvalue and ContainsKey. Because the contains method is easy to cause misunderstanding.

Hashtable inherits from the dictionary class, and HashMap is an implementation of the map interface introduced by Java1.2.

The biggest difference is that the Hashtable method is synchronize, and HashMap is not, when multiple threads access Hashtable, they do not need to synchronize their methods, and HashMap must provide it with external synchronization ( COLLECTIONS.SYNCHRONIZEDMAP).


The Hash/rehash algorithms used by Hashtable and HashMap are probably the same, so there is no big difference in performance.

HashMap Hashtable Difference

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.