When do I keep a map<Identifier, Object> vs a Collection<Object with identifier as field>

懵懂的女人 提交于 2021-01-08 02:01:15

问题


There is one question that I often ask myself while designing a program, and I am never quite sure how to answer it.

Let's say I have an object with multiple fields, amongst which there is one serving as the identifier to that specific object. Let's also say that I need to keep track of a List of such objects somewhere else.

I now have three, and probably even more, options on how to go about it:

  1. Have my object contain its own identifier, and all its other fields. I now use a simple array (or whatever simple list collection) of my objects where I need it. When I am looking for one specific object, I loop through my list and check for identifier equality.

Pros: 1. "Clarity" for each object instance. 2.? Cons: Manipulating a collection of these objects gets annoying

  1. Have my object contain all fields beside its identifier. I now use a Map with identifier as key, and object as value. When looking for one specific object, I just lookup the identifier in the map.

Pros: easy lookups and insertions,? Cons: object instance itself doesnt know what it is,?

  1. Combination of both: use a map with identifier as key and object having its own identifier as a field as value.

Pros: mentioned above. Cons: looks redundant to me.

What situations would call for what? Let's use the standard hello-world example of networking for example, a chat server: how would I handle multiple "groups/channels" people are in? What about other applications?


回答1:


Your question is very wide and, actually, contains two questions.

First is “Which data structure is better — dictionary or list?”. The answer is: it depends on performance you want to achieve on insertion and search operations. Basically if you need to look through the collection, then list is ok, and if you need to have fast look-up, then dictionary is better. Dictionary has more memory overhead than list.

The second is “Do I need to have an Id field inside an entity or can I use built in hash code?”. The answer is: it depends on how you will use your object. If you want Id just to store it in a dictionary, then, most likely, you can go with hash code. There is nothing wrong with storing Id of an entity inside that entity. Either you use Id or hash code, you need to be sure that this entity will be uniquely identified by id or hash. That's the main concern with it.

You can override GetHashCode method and make it return Id of your entity. Sometimes you can find such implementation when hash code is required for collection and Id is required for database.

So, it really doesn't matter what you will choose in the end if both approaches are working for you right now.




回答2:


A map<Identifier, Object> will offer you O(1) performance when retrieving an object based on its identifier. There certainly are situations where you want to achieve that.

However, in other cases it might be redundant to use this approach. It all depends on the situation at hand.




回答3:


Two guidelines may answer this question:

  1. A use case that calls for a lookup where there is an expectation of a 1:1 relationship between the key and value implies a Map structure.
  2. OOP implies that a key which is so closely related to an object as to preform a lookup should be encapsulated within that object.

Regarding the question of redundancy, consider the key in a map is nothing but an index. Indexes are as common in data as in books.



来源:https://stackoverflow.com/questions/47067047/when-do-i-keep-a-mapidentifier-object-vs-a-collectionobject-with-identifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!