HashMap resize method implementation detail

前端 未结 3 1957
傲寒
傲寒 2020-12-03 01:26

As the title suggests this is a question about an implementation detail from HashMap#resize - that\'s when the inner array is doubled in size. It\'s a bit word

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-03 01:50

    The design consideration has been documented within the same source file, in a code comment in line 211

    * When bin lists are treeified, split, or untreeified, we keep 
    * them in the same relative access/traversal order (i.e., field 
    * Node.next) to better preserve locality, and to slightly 
    * simplify handling of splits and traversals that invoke 
    * iterator.remove. When using comparators on insertion, to keep a 
    * total ordering (or as close as is required here) across 
    * rebalancings, we compare classes and identityHashCodes as 
    * tie-breakers. 
    

    Since removing mappings via an iterator can’t trigger a resize, the reasons to retain the order specifically in resize are “to better preserve locality, and to slightly simplify handling of splits”, as well as being consistent regarding the policy.

提交回复
热议问题