Does HashTable maintains the insertion order?

后端 未结 5 768
醉酒成梦
醉酒成梦 2021-01-07 18:57

The following code gives me the output in the same order of insertion. I read the javadoc and they did not even talk about the insertion order. Can someone help me to get th

相关标签:
5条回答
  • 2021-01-07 19:08

    Hashtable is used for fast lookup not for maintaining order. You should look into LinkedHashMap or other data structures.

    0 讨论(0)
  • 2021-01-07 19:12

    No, it does not. To preserve insertion order, instead use java.util.LinkedHashMap (javadoc).

    Also, HashMap is now preferred over Hashtable, because Hashtable has unnecessary concurrency overhead. (See Differences between HashMap and Hashtable?.)

    0 讨论(0)
  • 2021-01-07 19:24

    From Map Javadoc.

    The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.

    Also it's very useful to look inside the code of Hashtable and HashMap.

    0 讨论(0)
  • 2021-01-07 19:25

    LinkedHashMap is used for maintaining order of inserting elements.. Hashtable is similar to HashMap but it doesn't allow null key or value while HashMap allows one null key and several null values...

    0 讨论(0)
  • 2021-01-07 19:27

    no, it does not. it only knows the "hash" order. if you reorder the strings, you will find they still appear in the same order from the hashtable.

    0 讨论(0)
提交回复
热议问题