difference between linkedhashmap, hashmap, map, hashtable

前端 未结 3 2003
借酒劲吻你
借酒劲吻你 2021-02-04 22:55

I am preparing for software interviews and i am stuck with a question for days now.

I have not been able to figure out the difference between linkedhashmap, map, hashtab

3条回答
  •  [愿得一人]
    2021-02-04 23:23

    They all honor the same contract, but there are some differences in the implementation:

    • LinkedHashMap : keys are maintained in insertion order
    • HashTable : all operations are synchronized, no ordering guarantees
    • HashMap : no ordering guarantees, best performance

    Generally, the best practice is to use Map as the type for variables, and then you instantiate an implementing type based on the needs of your code. HashMap is generally preferred unless you need some ordering guarantees, in which case LinkedHashMap or TreeMap are good choices.

提交回复
热议问题