A hash table is an important way of storing and a common retrieval method. The basic idea is to use the value of the relationship code as an independent variable, calculate the corresponding function value by a certain function relation, interpret the value as the storage address of the node, and save the node to the storage unit corresponding to the storage address. The method of retrieving the key code is used in the retrieval. The hash table now has a complete set of algorithms for inserting, deleting, and resolving conflicts. In Java, the hash table is used to store objects for fast retrieval.
Java.util.Hashtable provides a way for users to use a hash table without having to consider how their hash tables really work.
There are three construction methods available in the Hashtable class, namely:
Public Hashtable ()
Public Hashtable (int initialcapacity)
Public Hashtable (int initialcapacity,float loadfactor)
The parameter initialcapacity is the initial capacity of the Hashtable, and its value should be greater than 0. Loadfactor, also known as the loading factor, is a float-type floating-point number between 0.0 and 0.1. It is a percentage that indicates when a hash table needs to be expanded, for example, there is a hash table with a capacity of 100 and a loading factor of 0.9, and when the capacity of the hash table 90% is already in use, the hash table is automatically expanded into a larger hash table. If the user does not assign these parameters, the system is automatically processed without the user's concern.
Hashtable provides basic methods for inserting, retrieving, and so on.
Insert
Public synchronized void put (Object key,object value)
Set a keyword key to the object value and add it to Hashtable. If this keyword already exists, the old object corresponding to this keyword is updated to the new object value. This indicates that the same keywords in the hash table cannot correspond to different objects (which is also obvious from the basic idea of the Hashtable).
Retrieve
Public synchronized object get (object key)
Gets the corresponding object based on the given keyword key.
Public synchronized Boolean containskey (Object key)
Determines whether the hash table contains the keyword key.
Public synchronized Boolean contains (Object value)
Determines whether value is an element in the hash table.
Delete
Public Synchronized object Remove (object key)
Removes the object from the hash table for the keyword key.
Public synchronized void Clear ()
Clear Hash Table
In addition, Hashtalbe also provides methods to get the corresponding enumeration collection:
Public synchronized enumeration keys ()
Returns the enumeration object corresponding to the keyword.
Public synchronized enumeration elements ()
Returns an enumeration object corresponding to an element.
Example 8.5 Hashtable.java gives an example of using Hashtable.
Example 8.5 Hashtalbe.java.
Import java.lang.*;
Import java.util.Hashtable;
Import java.util.Enumeration;
public class hashapp{
public static void Main (String args[]) {
Hashtable hash=new Hashtable (2, (float) 0.8);
Created a hash Table object hash with an initial capacity of 2 and a loading factor of 0.8
Hash.put ("Jiangsu", "Nanjing");
Give the string object "Jiangsu" a keyword "nanjing" and add it to the hash
Hash.put ("Beijing", "Beijing");
Hash.put ("Zhejiang", "Hangzhou");
System.out.println ("The Hashtable Hash1 is:" +hash);
System.out.println ("The size of this hash table is" +hash.size ());
Print the contents and size of the hash
Enumeration enum1=hash.elements ();
System.out.print ("the element of the hash is:");
while (Enum1.hasmoreelements ())
System.out.print (enum1.nextelement () + "");
System.out.println ();
Print the contents of the hash in turn
if (Hash.containskey ("Jiangsu"))
System.out.println ("The capatial of Jiangsu is" +hash.get ("Jiangsu"));
Hash.remove ("Beijing");
Delete a keyword Beijing corresponding object
System.out.println ("The Hashtable Hash2 is:" +hash);
System.out.println ("The size of this hash table is" +hash.size ());
}
}
Operation Result:
The Hashtable hash1 is: {beijing=beijing, Zhejiang=hangzhou, jiangsu=nanjing}
The size of this hash table is 3
The element of the hash is:beijing Hangzhou Nanjing
The capatial of Jiangsu is Nanjing
The Hashtable Hash2 is: {Zhejiang=hangzhou, jiangsu=nanjing}
The size of this hash table is 2
Hashtable is a subclass of the Dictionary (dictionary) class. In the dictionary class, the keyword corresponds to the data value. The dictionary class is an abstract class. There is also a class properties in Java.util, which is a subclass of Hashtable. It allows you to perform operations related to object properties.
Let's first look at the definition of 2 classes.
public class Hashtable extends Dictionary implements Map, Cloneable, java.io.Serializable
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:
1 Public synchronizedV Put (K key, V value) {//###### Note here 12 //Make sure the value was not null3 if(Value = =NULL) {//###### Note here 24 Throw Newnullpointerexception ();5 }6 //makes sure the key is not already in the Hashtable.7Entry tab[] =table;8 inthash = Key.hashcode ();//###### Note here 39 intIndex = (hash & 0x7FFFFFFF)%tab.length;Ten for(Entry e = Tab[index]; E! =NULL; E =e.next) { One if((E.hash = = hash) &&e.key.equals (Key)) { AV old =E.value; -E.value =value; - returnOld ; the } - } -modcount++; - if(Count >=threshold) { + //Rehash The table if the threshold is exceeded - rehash (); +tab =table; AIndex = (hash & 0x7FFFFFFF)%tab.length; at } - //creates the new entry. -Entry e =Tab[index]; -Tab[index] =NewEntry (hash, key, value, e); -count++; - return NULL; in}
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
1 PublicV Put (K key, V value) {//###### Note here 12 if(Key = =NULL)//###### Note here 23 returnPutfornullkey (value);4 inthash =Hash (Key.hashcode ());5 inti =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))) {9V OldValue =E.value;TenE.value =value; OneE.recordaccess ( This); A returnOldValue; - } - } themodcount++; -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 |
is synchronized |
Whether |
Is |
K,v can null |
Is |
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.
The difference between Hashtable and HashMap (prototype mode)