What hash algorithm does .net use? What about java?

As for the HashTable (and subsequent derivatives of this), does anyone know which hash algorithm uses .net and Java?

Are List and Dictionary as direct descandents hashtable?

+3
source share
7 answers

The hash function is not built into the hash table; the hash table calls the method on the key object to calculate the hash. Thus, the hash function varies depending on the type of key object.

Java a List - ( Map). List - ( , -), Java.

+6

.NET, Java.

Java -, , , hashCode() , - HashMap/ConcurrentHashMap (, ). , Hashtable Dictionary ( HashMap AbstractMap) . "- ".

String -, 31 . . , . "" -; , . , , String , . ( , " " -, .)

- - "", , - , -, , , - , - -, , " ", ​​ . - String , -, , . , - , Java HashMap "" - .. . , - , . ( - - .)

- . hashCode() .

+3

HASHING - , - HashTable.

HASHTABLE (, , ) - , HashTable -.

Java -.

+2

, HashTable - .NET, : GetHashCode().

, , , .

+1

.NET Reflector . -, , , -.

+1

.NET Dictionary<T> IEqualityComparer<T> - , . IEqualityComparer<T> Dictionary<T> ( ), , object.GetHashCode object.Equals.

How the standard implementation works GetHashCode, I'm not sure if it is documented. For specific types, you can read the source code of the method in Reflector or try checking the Rotor source code to see if it is.

+1
source

Looking for the same answer, I found this in the original source .net @ http://referencesource.microsoft.com .

     /*
      Implementation Notes:
      The generic Dictionary was copied from Hashtable source - any bug 
      fixes here probably need to be made to the generic Dictionary as well.
      This Hashtable uses double hashing.  There are hashsize buckets in the 
      table, and each bucket can contain 0 or 1 element.  We a bit to mark
      whether there been a collision when we inserted multiple elements
      (ie, an inserted item was hashed at least a second time and we probed 
      this bucket, but it was already in use).  Using the collision bit, we
      can terminate lookups & removes for elements that aren't in the hash
      table more quickly.  We steal the most significant bit from the hash code
      to store the collision bit.

      Our hash function is of the following form:

      h(key, n) = h1(key) + n*h2(key)

      where n is the number of times we've hit a collided bucket and rehashed
      (on this particular lookup).  Here are our hash functions:

      h1(key) = GetHash(key);  // default implementation calls key.GetHashCode();
      h2(key) = 1 + (((h1(key) >> 5) + 1) % (hashsize - 1));

      The h1 can return any number.  h2 must return a number between 1 and
      hashsize - 1 that is relatively prime to hashsize (not a problem if 
      hashsize is prime).  (Knuth Art of Computer Programming, Vol. 3, p. 528-9)
      If this is true, then we are guaranteed to visit every bucket in exactly
      hashsize probes, since the least common multiple of hashsize and h2(key)
      will be hashsize * h2(key).  (This is the first number where adding h2 to
      h1 mod hashsize will be 0 and we will search the same bucket twice).

      We previously used a different h2(key, n) that was not constant.  That is a 
      horrifically bad idea, unless you can prove that series will never produce
      any identical numbers that overlap when you mod them by hashsize, for all
      subranges from i to i+hashsize, for all i.  It not worth investigating,
      since there was no clear benefit from using that hash function, and it was
      broken.

      For efficiency reasons, we've implemented this by storing h1 and h2 in a 
      temporary, and setting a variable called seed equal to h1.  We do a probe,
      and if we collided, we simply add h2 to seed each time through the loop.

      A good test for h2() is to subclass Hashtable, provide your own implementation
      of GetHash() that returns a constant, then add many items to the hash table.
      Make sure Count equals the number of items you inserted.

      Note that when we remove an item from the hash table, we set the key
      equal to buckets, if there was a collision in this bucket.  Otherwise
      we'd either wipe out the collision bit, or we'd still have an item in
      the hash table.

       -- 
    */
+1
source

Source: https://habr.com/ru/post/1708840/


All Articles