Difference between HashMap, LinkedHashMap and TreeMap

后端 未结 17 2370
醉话见心
醉话见心 2020-11-22 01:01

What is the difference between HashMap, LinkedHashMap and TreeMap in Java? I don\'t see any difference in the output as all the three

17条回答
  •  野性不改
    2020-11-22 01:35

    Just some more input from my own experience with maps, on when I would use each one:

    • HashMap - Most useful when looking for a best-performance (fast) implementation.
    • TreeMap (SortedMap interface) - Most useful when I'm concerned with being able to sort or iterate over the keys in a particular order that I define.
    • LinkedHashMap - Combines advantages of guaranteed ordering from TreeMap without the increased cost of maintaining the TreeMap. (It is almost as fast as the HashMap). In particular, the LinkedHashMap also provides a great starting point for creating a Cache object by overriding the removeEldestEntry() method. This lets you create a Cache object that can expire data using some criteria that you define.

提交回复
热议问题