The difference between Java-hashmap and Hashtable

Source: Internet
Author: User
Tags rehash

class inheritance and implementation look up

 Public class Hashtable       extends Dictionary       Implements Map, Cloneable, java.io.Serializable  
 Public class HashMap       extends Abstractmap       Implements Map, Cloneable, Serializable  

Visible hashtable inherit from Dictiionary and HashMap inherit from Abstractmap

Compare from Put method

HashTable:

 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 the 3 method calls the Hashcode method of key, and if key==null, throws a null pointer exception

HASHMAP:

 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

Contains controversy
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.

Why is the contains method of Hashtable easy to cause misunderstanding?

True
The reason for this is that it is necessary to implement the Containsvalue in map:publicbooleanreturn contains ( value);} There's no difference in performance, so it's misleading to think that contains is a key that determines if there is an association

Default value size comparison

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 HashMap is 16, and must be a 2 index

Hashtable

     Public Hashtable () {        this (one, 0.75f);    } 

HashMap

     PublicHashMap () { This(default_initial_capacity, default_load_factor); }    /*** The default initial capacity-must be a power of. */    Static Final intdefault_initial_capacity = 1 << 4;//aka    /*** The load factor used when none specified in constructor. */    Static Final floatDefault_load_factor = 0.75f;


Conclusion: HashMap is preferred in most cases.

The difference between Java-hashmap and Hashtable

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.