Modification to HashMap after Android 5.0, androidhashmap

Source: Internet
Author: User

Modification to HashMap after Android 5.0, androidhashmap

Previously, it was found that the data in HashMap on the Android 5.0 host is different from that in Android 5.0, resulting in an interface in the project being faulty (the interface is cached, if the request parameter order changes, some data will not be available.) then, I checked the HashMap source code of Android 5.0 and Android 4.4, using meld to view the difference, we can see that google has modified the implementation of HashMap.

Android 5.0 source code on the left and Android 4.4 source code on the right






From the source code, we can see that Android 5.0 uses the following algorithm to calculate the HashCode of the key.

    private static int secondaryHash(int h) {        // Spread bits to regularize both segment and index locations,        // using variant of single-word Wang/Jenkins hash.        h += (h <<  15) ^ 0xffffcd7d;        h ^= (h >>> 10);        h += (h <<   3);        h ^= (h >>>  6);        h += (h <<   2) + (h << 14);        return h ^ (h >>> 16);    }

The HashCode algorithm used to calculate the Key in Android 4.4 is obviously different from that used in Android 5.0. Therefore, the same data sequence on the two systems is different after get. If you need to order the stored data, you can use the TreeMap built by the red/black tree.

    static int secondaryHash(Object key) {        int hash = key.hashCode();        hash ^= (hash >>> 20) ^ (hash >>> 12);        hash ^= (hash >>> 7) ^ (hash >>> 4);        return hash;    }


Reprinted please indicate the source: http://blog.csdn.net/l2show/article/details/46970507


Contact Us

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.