Questions about HashMap disturbances in the function


         Look at the recent jdk8 hashmap source, when you see this step when a little doubt, go online to search, I see all the articles are basically a copy of one of the (major community anyway, is currently such a situation) , that is to let the meaning of randomness is also involved in the high 16-bit computing, the result of an increase, decrease hash collision? ? ?

       At first glance seems to be the case, but grew more and more wrong; how I feel is that no matter how operations are not looked down upon the last few do, in which the probability of occurrence of each fixed length in the number of random or do not (1 / length); high after participating in operations can certainly guarantee is the same as the original low value more different, but can not guarantee the operation would have different values or unreasonable it. With a variety of questions, simply do the next experiment.
From the 4 th power of 2 to the power of 16, each of the original hash values and disturbance statistics and calculation functions radix
string value for the experimental data of 8 random, we usually use up key string as it
last Statistics results: 100 times the average collision rate of
the code:

import com.alibaba.fastjson.JSON;

        import java.util.ArrayList;
        import java.util.Random;
        import java.util.List;

/**
 * @description:
 * @author: wukong
 * @remark: create wukong 2019/12/26 22:49
 */
public class HashTest {
    public static void main(String[] args) {
        int length = 1 << 8;
        List<Double> doubles = new ArrayList<>(100);
        List<Double> double2s = new ArrayList<>(100);
        // 测试次数
        int count = 100;
        for (int i = 0; i < count; i++) {
            hashCalculate(length, doubles, double2s);
        }
        System.out.println("均值1:" + doubles.stream().mapToDouble((item) -> item).summaryStatistics().getAverage());
        System.out.println("均值2:" + double2s.stream().mapToDouble((item) -> item).summaryStatistics().getAverage());
        System.out.println("集合1:" + JSON.toJSON(doubles));
        System.out.println("集合2" + JSON.toJSON(double2s));
    }

    /**
     * @Description: hash碰撞率计算
     */
    private static void hashCalculate(int length, List<Double> doubles, List<Double> double2s) {
        int cardinal = length - 1;
        int load = (int) (length * 0.75);
        int crash = 0;
        int crash2 = 0;
        List<Integer> list = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        for (int0 = I; I <Load; I ++ ) {
             // random key hash value acquired 
            int the hash = getRandomString () the hashCode ();.
             // ANDed direct radix 
            int Result = & Cardinal the hash;
             // jdk8 in hashmap perturbation function 
            int disturbHash the hash ^ = (the hash >>> 16 );
             // value calculating perturbed 
            int result2 = Cardinal & disturbHash;
             // count the number of collisions is directly operation 
            IF (! list.contains (Result)) { 
                List. the Add (Result); 
            } the else { 
                Crash ++ ;
            } 
            // the number of collisions statistic disturbing 
            IF (! {List2.contains (result2)) 
                list2.add (result2); 
            } the else { 
                crash2 ++ ; 
            } 
        } 
        Double crashProbability = Crash / ( Double ) length;
         Double crashProbability2 = crash2 / ( Double ) length; 
        doubles.add (crashProbability); 
        double2s.add (crashProbability2); 
//       System.out.println ( "when the length" + length + "when, hash value calculation direct collision rate:" + crashProbability);
 //      System.out.println ( "when the length" + length + "When, after the disturbance function and the operation rate of the collision:" + crashProbability2); 
    } 

    / ** 
     * @Description: to obtain random key string 
     * / 
    Private  static String getRandomString () { 
        String STR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ; 
        the Random Random = new new the Random (); 
        the StringBuffer SB = new new the StringBuffer ();
         int length =. 8 ;
         for ( int I = 0; I <length; I ++ ) {
             int Number = Random .nextInt (62 );
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }
}

Results exemplary operation (hereinafter, the length of the 16 power of 2):

After finishing the data is complete, do a line chart:


According to the results, I conclude my consistency and ideas, both collision rate should be consistent, this perturbation function seems to be useless, more precisely, this step should be a function of all disturbances it is useless. . . . .
After I finished the test, I do not know Joy and sorrow, how people design there will be problems, there are more people who agree with that kind of design, but I was more convinced that this step is unlikely to reduce collision rates. . . . I do not know where I would like biased, wrong, or because of what did not learn probability theory, or experiment where the wrong time, I hope you can to correct the wrong idea

Guess you like

Origin www.cnblogs.com/wukonga1234/p/12112511.html