In Java, which is the most recommended class for dictionary data structures?

I need a data structure to store users that should be retrieved by ID

Solution

It may depend on how many users you plan to have and whether you need to order or get individual items only by ID

HashMap uses hash code to store things, so you have continuous put and get operation time, but the items are always out of order

Treemap uses a binary tree, so you have log (n) time for basic operations, but the items remain in order in the tree

I will use HashMap because it is simpler (remember to give it an appropriate initial capacity) Remember that these data structures are not synchronized by default, and if you plan to use it in multiple threads, use concurrenthashmap

The middle method is LinkedHashMap, which uses the same structure as HashMap (hashcode and equals methods), but it also retains the list of double linked elements inserted into the map (maintaining the insertion order) This mixture has ordered items (sorted in insertion order, as suggested in the comments... Accurate, but I have specified this) without the performance loss of treemap

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>