HashMap, LinkedHashMap and TreeMap: Do you really know them?

Dear friends, hello everyone! I am Xiaomi, a post-90s programmer who is keen on technology sharing. Today I want to talk to you about a topic that is often asked in interviews: the difference between HashMap, LinkedHashMap, and TreeMap. This is a very important knowledge point. It is not only frequently mentioned in interviews, but also often used in actual development. Let’s take a closer look at the similarities and differences between these three!

HashMap

First, we start with HashMap. HashMap is a very commonly used data structure in Java, which implements the Map interface, allowing us to associate keys and values ​​together. HashMap is characterized by fast and efficient, and it uses a hash table to store data.

Features:

  • Disorder: The key-value pairs in HashMap are stored out of order and will not be arranged in any order.
  • Allow null keys and null values: HashMap allows both keys and values ​​to be null.
  • Efficient performance: The insertion, deletion, and search operations of HashMap are very efficient, and the average time complexity is O(1).
  • Not thread-safe: HashMap is not thread-safe, and additional synchronization measures are required if used in a multi-threaded environment.

scenes to be used:

HashMap is suitable for most cases, especially when we only care about the storage and retrieval of key-value pairs and do not care about their order, HashMap is a good choice.

LinkedHashMap

Next, let's take a look at LinkedHashMap. LinkedHashMap is also a commonly used Map implementation, which inherits from HashMap and maintains the insertion order.

Features:

  • Orderedness: LinkedHashMap maintains the insertion order of key-value pairs, so it will be output in the order of insertion when traversing.
  • Allow null keys and null values: Like HashMap, LinkedHashMap also allows both keys and values ​​to be null.
  • Efficient performance: The performance of LinkedHashMap is similar to that of HashMap, and the average time complexity of insertion, deletion, and search operations is O(1).
  • Not thread-safe: LinkedHashMap is not thread-safe, and needs to be synchronized in a multi-threaded environment.

scenes to be used:

When we need to maintain the insertion order, we can choose to use LinkedHashMap. It is often used to implement the LRU cache eviction algorithm because it can be easily implemented through the characteristics of LinkedHashMap.

TreeMap

Finally, let's look at TreeMap. TreeMap is an implementation based on red-black trees, which can sort and store key-value pairs.

Features:

  • Orderliness: TreeMap sorts and stores keys, so key-value pairs are ordered. You can sort by the natural order of keys or by a Comparator.
  • Null keys are not allowed: TreeMap does not allow keys to be null, and a NullPointerException will be thrown if a null key is inserted.
  • Efficient performance: The average time complexity of TreeMap's insertion, deletion, and search operations is O(log N) because it is implemented based on red-black trees.
  • Not thread-safe: Like the previous two Map implementations, TreeMap is not thread-safe.

scenes to be used:

When we need to sort and store key-value pairs, we can choose to use TreeMap. It is often used in scenarios where data needs to be traversed in key order.

Summarize

So, what are the similarities and differences between HashMap, LinkedHashMap, and TreeMap?

  • HashMap is unordered, suitable for key-value pair storage and retrieval in most cases, and has efficient performance.
  • LinkedHashMap maintains the insertion order, which is suitable for scenarios where order needs to be maintained, and the performance is also very good.
  • TreeMap ordered storage is suitable for scenarios that require sorting. The performance is relatively low, but it performs better in large data collections.

When choosing to use them, make a reasonable choice based on specific needs. If you only care about the storage and retrieval of key-value pairs, and don't need to maintain order, then a HashMap is probably the best choice. If you need to maintain insertion order or sort keys, consider using LinkedHashMap or TreeMap .

END

I hope this article can help you better understand the differences between HashMap, LinkedHashMap and TreeMap, and also help you behave more calmly and professionally during interviews. If you have any questions or want to know more about Java, please leave a message and I will try my best to answer your questions. In addition, everyone is also welcome to follow my official account [ Know what it is and why it is ], learn technology together, and share growth!

おすすめ

転載: blog.csdn.net/en_joker/article/details/132732833