Java face test--detailed hashmap and Hashtable differences _java

Source: Internet
Author: User
Tags rehash

I. The difference between HASHMAP and Hashtable

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

 public class Hashtable 
  extends Dictionary 
  
public class HashMap 
 extends Abstractmap 
 

Visible Hashtable inherits from Dictiionary and HashMap inherits from Abstractmap

Hashtable's Put method is as follows

Public synchronized V-put (K key, V value) {//###### Note here 1//Make sure the "value is" not 
 null 
 if (value = = null) { ###### Note here 2 
 throw new NullPointerException (); 
 } 
 Makes sure the ' key is ' not ' already in the Hashtable. 
 Entry tab[] = table; 
 int hash = Key.hashcode (); ###### Note here 3 
 int index = (hash & 0x7fffffff)% Tab.length; 
 for (Entry e = Tab[index]; e!= null; e = e.next) { 
 if ((E.hash = hash) && e.key.equals (key)} { 
  V old = E.value; 
  E.value = value; 
  return old; 
 } 
 modcount++; 
 if (count >= threshold) { 
 //Rehash The table if the threshold is exceeded 
 Rehash (); 
 tab = table; 
 Index = (hash & 0x7fffffff)% Tab.length; 
 } 
 Creates the new entry. 
 Entry e = Tab[index]; 
 Tab[index] = new Entry (hash, key, value, e); 
 count++; 
 return null; 
}

Note that the 1 method is synchronized

Note that the 2 method does not allow Value==null

Note that the 3 method calls the Hashcode method of the key and, if key==null, throws a null pointer exception HashMap The PUT method as follows

Public V-Put (K key, V value) {//###### Note here 1 
 if (key = = null)//###### Note here 2 return 
 Putfornullkey (value); 
 int hash = hash (Key.hashcode ()); 
 int i = indexfor (hash, table.length); 
 for (Entry e = table[i]; e!= null; e = e.next) { 
 Object k; 
 if (E.hash = = Hash && ((k = e.key) = = Key | | key.equals (k))) { 
  V oldValue = e.value; 
  E.value = value; 
  E.recordaccess (this); 
  Return OldValue 
 } 
 } 
 modcount++; 
 AddEntry (hash, key, value, I); ###### Note here return 
 null; 
}

Note that the 1 method is unsynchronized

Note that the 2 method allows key==null

Note that 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 has been removed

Of course, all 2 classes use ContainsKey and Containsvalue methods.

HashMap Hashtable
Parent class Abstractmap Dictiionary
Whether to sync Whether Is
K,v can null Is Whether

HashMap is a lightweight implementation of Hashtable (not thread-safe implementations), and they all complete the map interface, with the main difference being that hashmap allows null (NULL) key values (key), which may be more efficient than hashtable due to non thread-safe.

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

HashMap the 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 an external synchronization ( COLLECTIONS.SYNCHRONIZEDMAP).

Hashtable and HashMap adopt the Hash/rehash algorithm are probably the same, so there is no significant difference in performance.

Summarize:

The key value in HashMap is allowed to be null and is not synchronized

Hashtable the key value is not allowed to null is synchronized

Inheritance is different, but all of them implement the map interface

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.