Is HashMap the best choice?
This question relates to using most efficient data structure for a part of a uni-project. I have to store several instruction objects in a data structure. Each instruction has a unique int ID called Stage. Is HashMap the best choice to find the instruction i need fast ?I havent used it before but from the description it seems that using the int ID as key would make this run efficiently. If you can, please suggest a more efficient way to do it开发者_如何转开发. Thanks
If you only want to lookup entries and not add/delete move, sort or do anything else, than an array is the fastest data structure for this.
Yes. Some kind of Map
seems to be the data structure of choice in your scenario.
Note that a HashMap
does not maintain the order of its elements. If order is important to you, I suggest you use LinkedHashMap
(or perhaps even some List
structure) instead.
I think that's the best way because that way you can access the table in O(1). Depends also on what's the type of your ids, maybe an array is enough (and even more efficient), but generally a hash-table is more flexible for these purposes.
If you know the domain of the keys, an Arraylist or a plain array may be even more efficient. But there are reasons not to use plain arrays too much.
If the ID's are simply Integers and they are like 0,1,2..n then an array would be the best choice.
精彩评论