Will similar Strings in HashMap lead to increased chance of collisions?

后端 未结 1 778
囚心锁ツ
囚心锁ツ 2021-01-29 11:21

Consider the following:

HashMap hm = new HashMap<>();
final String prefix = \"My objects \";
int counter = 0;

void put(Object value)         


        
相关标签:
1条回答
  • 2021-01-29 11:48

    No it will not. And that is not necessarily because of String#hashcode; but because a HashMap will re-hash whatever your hashcode is by XOR-ing firs 16 bits with the last 16.

    // this is re-hashing that is done internally
    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }
    

    But even if that would increase collision, you might never feel it. For small buckets/bin where entries are placed one after another (in a linked fashion), equals will be called to get the actual entry you care about.

    In case a certain bin/bucket reaches a certain threshold, it will be transformed in a perfectly balanced tree node. The search time in such a tree is 0(logn).

    Even if the same entries report the same hashcode after the re-hash, a map has still to decide which entry is bigger in case of a tie.

    It would then try to invoke Comparable#compareTo in case your Keys implement Comparable. In case they do not implement Comparable, System.identityHashcode will be called to decide in case of a tie.

    As you say from a performance perspective because of all these internal things, your average search time will be O(1) in the Map.

    0 讨论(0)
提交回复
热议问题