Hashtable and HashMap in the difference between C # - very detailed

C # and the difference in the HashMap Hashtable - very detailed; from Reference

                                                http://www.hd1204.com/article/html/1655.html thanks to the original;

 

HashTable is widely used, HashMap HashTable class that is in place for the new framework, that recommend the use of HashMap, do not use the HashTable. You might think HashTable useful, why not?

    Here a brief analysis of the difference between them.

  1.HashTable methods are synchronized, HashMap without synchronization, so multi-threaded applications to manually synchronize HashMap this distinction as Vector and ArrayList same.

  2.HashTable not allow null values ​​(key and value can not), HashMap allows null values ​​(key and value can be).

  3.HashTable a contains (Object value), the function and containsValue (Object value) function the same.

  4.HashTable use Enumeration, HashMap use Iterator. These are just different surfaces, their implementation is also very different.

  5.HashTable the hash array 11 is the default size, increasing manner is old * 2 + 1. The default size of the HashMap hash array 16, and must be a power of two.

  6. The use of different hash values, HashTable object directly hashCode, the code is such that:

  int hash = key.hashCode();

  int index = (hash & 0x7FFFFFFF) % tab.length;

  And HashMap recalculated hash value, and replaced with a 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);

  }

  These are just some of the more prominent difference, of course, there are many different, such as HashMap null operations on their implementation.

  Hashtable and HashMap difference:

  1.Hashtable Dictionary is a subclass, HashMap is an implementation of the Map interface;

  Method 2.Hashtable are synchronized, and a method of HashMap default is asynchronous. In other words, in a multi-threaded application, without special operation to safely use the Hashtable; and for HashMap, the need for additional synchronization mechanism. But HashMap synchronization problems can be solved by a static method of Collections:

  Map Collections.synchronizedMap(Map m)

  This method returns a synchronization of the Map, the HashMap Map encapsulates all the underlying methods, such underlying HashMap even in a multi-threaded environment is safe.

  3. In the HashMap, as null key, only one such bond; can have one or more keys corresponding to the value of null. When the get () method returns a null value, i.e., may not represent the HashMap key, it indicates that the bond may be the corresponding value of null. Thus, in a HashMap can not get () method to determine whether there is a key HashMap rather should containsKey () method to determine.

  Dictionary class inherited from Hashtable, and HashMap of Java

   Simple hash table
   to add a key value in a hash table keyvalue of: HashtableObject.Add (key, value); 
   removing a keyvalue in the hash table key-value pair: HashtableObject.Remove (key); 
   from the hash remove all of the elements in the table: HashtableObject.Clear (); 
determining whether the hash table contains a specific key key: HashtableObject.Contains (key); 
following console program will contain all of the above operations:

View Code
Copy the code
the using the System; 
the using the System.Collections; Hashtable used when file, must be introduced into the namespace
class Hashtable
{
public static void the Main ()
{
Hashtable HT
= new new Hashtable (); to create a file Hashtable instance
ht.Add (E, e); Add keyvalue key-value pair
ht.Add (A, A);
ht.Add (C, C);
; ht.Add (B, B)

String S = ( String HT) [A];
IF (ht.Contains (E )) File determines whether the hash table contains a specific key, which returns true or to false
Console.WriteLine (The E keyexist);
ht.Remove (C); removal of a key-value pair keyvalue
Console.WriteLine (ht [a] ); where output A
ht.Clear (); Removes all elements
Console.WriteLine (ht [A]); file will not have any output here
}
}

// three traversing the hash table
// traverse need to use a hash table DictionaryEntry Object, the following code: for (de the DictionaryEntry in HT ) fileht example of a Hashtable { Console.WriteLine (de.Key); de.Key value pairs corresponding to keyvalue Key Console.WriteLine (de.Value); de.Key keyvalue value pairs corresponding value } // IV. hash table sorting / * hash table is sorted here is the definition of keyvalue key-value pairs in key rearranged according to certain rules, but in fact this definition can not be realized, because we can not directly in the Hashtable key to be rearranged, if necessary to provide a certain rule output Hashtable, an alternative approach may be used: * / the ArrayList akeys = new new (ht.Keys) the ArrayList; import File forget the System.Collections akeys.Sort () ; file sorted alphabetically for (














String SKey in akeys)
{
Console.Write (SKey
+ );
Console.WriteLine (HT [SKey]); sorted output
}
Copy the code

  

Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2012/07/19/2599557.html

Guess you like

Origin blog.csdn.net/weixin_34253126/article/details/93495135