Java – is it a good habit to use HashMap to index object lists?

I need to find objects through properties

One option is to iterate over the array of objects and check each object whose properties match

Another option is to put the object in the hash map with the attribute as the key You can then simply retrieve the object through properties

Although you copied the attribute data, is the second option a good practice?

Note: this attribute is assumed to be unique

Solution

Yes! Depending on what you provide, it is usually better to use map A value found in the map (where the key has a good hash function) is O (1) Finding elements in an array or list is O (n)

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
分享
二维码
< <上一篇
下一篇>>