HashMap should be unsorted but still sorts according to key

前端 未结 9 2057
余生分开走
余生分开走 2021-01-06 06:08

According to these:

  1. http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html
  2. Difference between HashMap, LinkedHashMap and TreeMap
  3. jav
9条回答
  •  甜味超标
    2021-01-06 06:58

    Since no answer really utilized looking at the Java source, I will do so! :)

    When you call the put() function, the internal hash function uses the hashCode of the object, to generate the hashing index. [put() source]

    The hash() function simply ensures that hashCodes that differ only by constant multiples at each bit position have a bounded number of collisions [use Google to see why that is the case].

    Things just coincidentally worked here. That is about it.

提交回复
热议问题