# JAVA8 Hash Algorithm

Source: Internet
Author: User

First, hash algorithm

The hashing algorithm maps a binary value of any length to a shorter fixed-length binary value, a small binary value called a hash value. A hash value is a unique and extremely compact numeric representation of a piece of data. If you hash a clear text and even change only one letter of the paragraph, subsequent hashes will produce different values. To find two different inputs that hash the same value, it is not possible to compute, so the hash value of the data can verify the integrity of the data. Typically used for quick find and encryption algorithms.

Second, the JDK hash algorithm implementation

(1) Interger

`    Private Final int value;     @Override    publicint  hashcode () {        return  Integer.hashcode (value);    }     Public Static int hashcode (int  value) {        return  value;    }`

The hash algorithm of integer is to get its value directly. int integer range is very large, scattered wide conflict small.

(2) Short

`    Private Final  Short value;    @Override    publicint  hashcode () {        return  short.hashcode ( value);    }      Public Static int hashcode ( short value) {        return (int) value;    } `

(3) Byte

`    Private Final byte value;    @Override    publicint  hashcode () {        return  byte.hashcode ( value);    }      Public Static int hashcode (byte  value) {        return (int) value;    } `

(4) Long

`    Private Final Long value;    @Override    publicint  hashcode () {        return  long.hashcode ( value);    }          Public Static int hashcode (long  value) {        return (int) (value ^ (value >>>));    }`

The long type is too large for the index range and needs to be converted to int type. Simply getting the low 32 bits here is easy to cause the hash to be uneven, because the high part is not being exploited. So this takes the logical right shift 32 bits, so that the high 32-bit and low 32-bit XOR operation, resulting in high-level low can be exploited to

(5) Double

`    Private Final Double value;    @Override    publicint  hashcode () {        return  double.hashcode ( value);    }      Public Static int hashcode (double  value) {        long bits = doubletolongbits ( value);         return (int) (Bits ^ (bits >>> a));    }`

Because double cannot be indexed, it needs to be converted to an integer

The IEEE floating-point standard encoding is used because the double data type is represented by a 64-bit bit code. If you use it as a 8-byte integer encoding, you can get a long type of number

The long type is too large for the index range and needs to be converted to int type. Simply getting the low 32 bits here is easy to cause the hash to be uneven, because the high part is not being exploited. So this takes the logical right shift 32 bits, so that the high 32-bit and low 32-bit XOR operation, resulting in high-level low can be exploited to

The last obtained number strongly turns int, leaving only the low 32 bits that have been fully disturbed

(6) Float

`     Private Final floatvalue; @Override Public inthashcode () {returnFloat.hashcode (value); }     Public Static intHashcode (floatvalue) {        returnfloattointbits (value); }     Public Static intFloattointbits (floatvalue) {        intresult =floattorawintbits (value); //Check for NaN based on values of bit fields, maximum//exponent and nonzero significand.        if((Result & floatconsts.exp_bit_mask) = =floatconsts.exp_bit_mask)&&(Result& floatconsts.signif_bit_mask)! = 0) Result= 0x7fc00000; returnresult; }     Public Static native intFloattorawintbits (floatValue);`
` Public class floatconsts {    publicstaticfinalint exp_bit_mask = 2139095040 ;      Public Static Final int Signif_bit_mask = 8388607;     // ... }`

(7) Boolean

`    Private Final Boolean value;    @Override    publicint  hashcode () {        return  Boolean.hashcode (value);    }      Public Static int hashcode (boolean  value) {        return value? 1231:1237;    } `

(8) Character

`    Private Final Char value;    @Override    publicint  hashcode () {        return  Character.hashcode (value);    }      Public Static int hashcode (char  value) {        return (int) value;    } `

(9) String

`    Private Final Charvalue[];  Public inthashcode () {inth =Hash; if(h = = 0 && value.length > 0) {            CharVal[] =value;  for(inti = 0; i < value.length; i++) {h= * H +Val[i]; } Hash=h; }        returnh; }`

(Ten) Object

`     Public native int hashcode ();`

(11) Custom Objects

` public  class  Node<t> { private   T data;  private  node<t> next = null         Span style= "COLOR: #000000" >; @Override  public  int   Hashcode () { int  hash = 3; Hash  = \$ * hash + objects.hashcode (this  .d        ATA); Hash  = \$ * hash + objects.hashcode (this   return   hash; }}`
` Public Final class Objects {    publicstaticint  hashcode (Object o) {        return  null ? O.hashcode (): 0;    }     // ...}`

(HASHMAP)

`    Static Final intHash (Object key) {inth; return(Key = =NULL) ? 0: (H = key.hashcode ()) ^ (H >>> 16); }     Public inthashcode () {intH = 0; Iterator<Entry<K,V>> i =EntrySet (). iterator ();  while(I.hasnext ()) H+=I.next (). Hashcode (); returnh; }`

(Hashtable)

`    Public synchronizedv put (K key, V value) {//Make sure the value was not null        if(Value = =NULL) {            Throw NewNullPointerException (); }        //makes sure the key is not already in the Hashtable.entry<?,? > tab[] =table; inthash =Key.hashcode (); intIndex = (hash & 0x7FFFFFFF)%tab.length; @SuppressWarnings ("Unchecked") Entry<K,V> entry = (entry<k,v>) Tab[index];  for(; Entry! =NULL; Entry =entry.next) {if((Entry.hash = = hash) &&entry.key.equals (Key)) {V old=Entry.value; Entry.value=value; returnOld ;        }} addentry (hash, key, value, index); return NULL; }     Public synchronized inthashcode () {intH = 0; if(count = = 0 | | Loadfactor < 0)            returnH//Returns ZeroLoadfactor=-loadfactor;//Mark hashcode computation in progressentry<?,? >[] tab =table;  for(entry<?,? >Entry:tab) {             while(Entry! =NULL) {h+=Entry.hashcode (); Entry=Entry.next; }} loadfactor=-loadfactor;//Mark hashcode Computation complete        returnh; }`

JAVA8 Hash Algorithm

Related Keywords:

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.