HashMap should be unsorted but still sorts according to key

前端 未结 9 2091
余生分开走
余生分开走 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:44

    You can't assume that it will be sorted. The reason why it appears sorted, in this simple example: A HashMap is internally constructed from "Bins". These Bins contain the actual elements. They are basically small lists that reside in an array.

    [0] -> [ Bin0: ... ]
    [1] -> [ Bin1: ... ]
    [2] -> [ Bin2: ... ]
    [3] -> [ Bin3: ... ] 
    

    When an item is inserted into the HashMap, then the "Bin" in which it should be inserted is - to simplify it a little - found by using the hashCode() of the object as an array index. For example, if the hashCode is 2, it will be inserted into Bin 2. When this "index" is greater than the array size, it will be placed into Bin (index%arraySize) - that is, if the hashCode is 5, it will be inserted into Bin 1.

    And since a HashMap initially has an internal array size of 10, inserting Integer objects between 0 and 9 will coincidentally place the elements in the right order into the array. (The hashCode of an Integer is just its value, of course).

    (Note: The actual algorithms and hash functions may be slightly more complicated, but that's the basic idea)

提交回复
热议问题