Is the order of values retrieved from a HashMap the insertion order

前端 未结 6 2000
天命终不由人
天命终不由人 2020-11-22 07:26

I am trying figure out the order in which the values in a HashMap are/can be retrieved. Heres the code snippet for the same.

import java.util.HashMap;

publ         


        
6条回答
  •  执笔经年
    2020-11-22 08:19

    From the Javadoc: HashMap "class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time."

    If you need consistent ordering, you can use LinkedHashMap (for insertion/access order), or TreeMap (for comparision order). Please note, that these maintain the order of the keys, not the values.

提交回复
热议问题