C # hashtable instructions and differences between hashtable and hashmap

Source: Internet
Author: User

1. Brief description of the hash table (hashtable)

In. in the. NET Framework, hashtable is system. A container provided by the collections namespace is used to process and present key-value pairs similar to key/value. The key is usually used for quick search, and the key is case sensitive; value is used to store the value corresponding to the key. In hashtable, key/value pairs are of the object type, so hashtable can support any type of key/value pairs.

 

Ii. Simple operations on Hash Tables

Add a key/value pair in the hash table: hashtableobject. Add (Key, value );
Remove a key/value pair in the hash table: hashtableobject. Remove (key );
Remove all elements from the hash table: hashtableobject. Clear ();
Determine whether the hash table contains the specified key: hashtableobject. Contains (key );
The following console contains all the preceding operations:
Using system;
Using system. collections; // This namespace must be introduced when hashtable is used.
Class hashtable
{
Public static void main ()
{
Hashtable ht = new hashtable (); // create a hashtable instance
Ht. Add ("e", "E"); // Add key/value pairs
Ht. Add ("A", "");
Ht. Add ("C", "C ");
Ht. Add ("B", "B ");
String S = (string) HT ["A"];
If (HT. Contains ("e") // checks whether the hash table contains a specific key. The return value is true or false.
Console. writeline ("the e key: exist ");
Ht. Remove ("C"); // remove a key/value pair
Console. writeline (HT ["A"]); // output
Ht. Clear (); // remove all elements
Console. writeline (HT ["A"]); // there will be no output here
}
}
 
3. traverse the hash table

Dictionaryentry object is required to traverse a hash table. The Code is as follows:
For (dictionaryentry de in HT) // HT is a hashtable instance.
{
Console. writeline (De. Key); // de. Key corresponds to the key-Value Pair
Console. writeline (De. Value); // de. Key corresponds to the key/value Key-Value Pair Value
}

4. Sort hash tables

The definition of sorting hash tables here is to re-arrange the keys in key/value pairs according to certain rules, but in fact this definition cannot be implemented, because we cannot re-arrange keys directly in hashtable, if hashtable needs to provide output of certain rules, we can adopt a work und:
Arraylist akeys = new arraylist (HT. Keys); // do not forget to import system. Collections
Akeys. Sort (); // sort by letter
For (string skey in akeys)
{
Console. Write (skey + ":");
Console. writeline (HT [skey]); // output after sorting
}


Differences between hashtable and hashmap
Hashtable is widely used. hashmap is a class used to replace hashtable in the new framework. In other words, hashmap is recommended instead of hashtable. Maybe you think hashtable is very useful. Why not? Here we will briefly analyze their differences.

1. The hashtable method is synchronous, And the hashmap is not synchronized. Therefore, the difference between manual synchronization of hashmap in multithreading is like that of vector and arraylist.

2. hashtable does not allow null values (neither key nor value). hashmap allows null values (both key and value ).

3. hashtable has a contains (object value), which has the same functions as containsvalue (object value.

4. hashtable uses enumeration and hashmap uses iterator. The above is just a difference on the surface, and their implementation is also very different.

5. The default size of the hash array in hashtable is 11, and the increase is in the old * 2 + 1 mode. The default size of the hash array in hashmap is 16, and it must be an index of 2.

6. For different hash values, hashtable directly uses the hashcode of the object. The Code is as follows:

Int hash = key. hashcode ();

Int Index = (hash & 0x7fffffff) % tab. length;

Hashmap recalculates the hash value and replaces the modulo:

Int hash = hash (k );
Int I = indexfor (hash, table. Length );

Static int Hash (Object X ){
Int H = x. hashcode ();
H + = ~ (H <9 );
H ^ = (H >>> 14 );
H + = (H <4 );
H ^ = (H >>> 10 );
Return h;
}

Static int indexfor (int h, int length ){
Return H & (length-1 );
}
The above are just some of the outstanding differences. Of course, there are still many different implementations, such as hashmap's operations on null.

Differences between hashtable and hashmap:
1. hashtable is a subclass of dictionary, and hashmap is an implementation class of the map interface;
2. Methods in hashtable are synchronized, while those in hashmap are not synchronized by default. That is to say, hashtable can be safely used in multi-threaded applications without special operations. For hashmap, additional synchronization mechanisms are required. However, the synchronization problem of hashmap can be solved through a static method of collections:
Map collections. synchronizedmap (MAP m)
This method returns a synchronous map, which encapsulates all the underlying hashmap methods, making the underlying hashmap safe even in a multi-threaded environment.
3. In hashmap, null can be used as a key, and there is only one such key. One or more keys can correspond to null values. When the get () method returns a null value, it can indicate that the key does not exist in hashmap or that the corresponding value of the key is null. Therefore, the get () method cannot be used to determine whether a key exists in the hashmap. Instead, the containskey () method should be used to determine whether a key exists in the hashmap.

Hashtable inherits from the dictionary class, while hashmap is an implementation of the map interface introduced by java1.2.

Hashmap allows null as the key or value of an entry, while hashtable does not.

In addition, hashmap removes the hashtable contains method and changes it to containsvalue and containskey. The contains method is easy to misunderstand.
The biggest difference is that the hashtable method is synchronize, but hashmap is not.
When multiple threads access hashtable, they do not need to implement synchronization for their methods, but hashmap
You must provide external synchronization for it.

The hash/rehash algorithms used by hashtable and hashmap are roughly the same, so there is no big difference in performance.

From: http://www.cnblogs.com/happy25/archive/2011/02/17/1956643.html

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.