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

前端 未结 6 1933
天命终不由人
天命终不由人 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

    No, the order is not preserved in case of HashMap (if you want sorted implementation.) In case you want keys to be sorted, you can use TreeMap.

    for example - {[3=1],[2=50],[20=4],[14=1]} -> HashMap

    for TreeMap, you get - {[2=50],[3=1],[14=1],[20=4]} -> TreeMap

提交回复
热议问题