current position:Home>[Java data structure & algorithm] I'd rather kill myself than others

[Java data structure & algorithm] I'd rather kill myself than others

2022-01-26 23:12:25 I'm Xiaobai

【Java data structure & Algorithm 】️ I'd rather be tired to death , Also kill others 10️ Hash implementation

summary

Starting today , Xiaobai, I will take you to open Java data structure & A new chapter in algorithms .

 Insert picture description here

Get hash value

hashCode() Method can return the hash value of an object . It should be noted that , We need to box the values , To call hashCode() Method .

 Insert picture description here

Example :

public static void main(String[] args) {

    //  decimal 
    Integer a = 1;
    System.out.println(a.hashCode());

    //  negative 
    Integer b = -1;
    System.out.println(b.hashCode());
        
    //  decimal 
    Double c = 1.23;
    System.out.println(c.hashCode());

    //  character string 
    String d = "Hello World";
    System.out.println(d.hashCode());
}

Output results :

1
-1
1158867386
-862545276

Hash Collisions

Hash Collisions (Hash Collision) The reason for this is that the number of hash algorithms is infinite , However, the range of calculated results is limited . Therefore, two different data will get the same hash value , Hash conflict .

 Insert picture description here

How to deal with hash conflict :

  • Chain address : Will have the same hash It's worth it key Put it in the same bucket
  • Open address method : Will have the same hash It's worth it key The last value of moves backward to the empty position

copyright notice
author[I'm Xiaobai],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201262312223818.html

Random recommended