The difference between--hashmap and Hashtable in Java Surface test questions

Source: Internet
Author: User
Tags rehash

I. The difference between HASHMAP and Hashtable

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

1  Public class Hashtable   2     extends Dictionary   3     Implements
 Public class HashMap       extends Abstractmap       Implements Map, Cloneable, Serializable  

Visible hashtable inherit from Dictiionary and HashMap inherit from Abstractmap

  The Put method of Hashtable is as follows

 Public synchronizedV Put (K key, V value) {//###### Note here 1//Make sure the value was not null  if(Value = =NULL) {//###### Note here 2    Throw NewNullPointerException (); }    //makes sure the key is not already in the Hashtable. Entry tab[] =table; inthash = Key.hashcode ();//###### Note here 3  intIndex = (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; returnOld ; }} Modcount++; if(Count >=threshold) {      //Rehash The table if the threshold is exceededrehash (); tab=table; Index= (hash & 0x7FFFFFFF)%tab.length; }    //creates the new entry. Entry e =Tab[index]; Tab[index]=NewEntry (hash, key, value, E); Count++; return NULL; }  

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

 PublicV Put (K key, V value) {//###### Note here 1  if(Key = =NULL)//###### Note here 2    returnPutfornullkey (value); inthash =Hash (Key.hashcode ()); inti =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); returnOldValue; }} Modcount++;  AddEntry (hash, key, value, I); //######, watch this .  return NULL; }  

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.

  Summary :

     The HashMap Medium key value is allowed to be empty and is not synchronized

     Hashtable Medium key value is not allowed to be null is synchronous

     Inherit different, but all implement the map interface

The difference between--hashmap and Hashtable in Java Surface test questions

Related Article

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.