我有许多 X 类型的自定义对象。X 有许多参数,并且在集合中必须是唯一的。 (我根据自定义参数创建了自己的 equals 方法来检查这一点)
在每个 x 类型的对象中,我有一个对象 y 的列表。
我想轻松添加/删除/修改对象 y。
例如:
要编写 add 方法,它将类似于 add(objTypeX, objTypeY)
我会检查集合是否已经有 objTypeX。
如果是这样:我会将 objTypeY 添加到已经存在的 objTypeX
否则:我将创建 objTypeX 并将 objTypeY 添加到该对象。
要修改 objTypeY,它将类似于 (objTypeX, objTypeY, newobjTypeY)
我将从集合中获取 objTypeX 并将 objTypeY 修改为 newobjTypeY
我应该使用哪些集合?我尝试使用哈希集,但我可以从列表中获取特定对象,而无需遍历列表直到找到该对象。
我在 vb.net 3.5 中开发这个
I have a number of custom objects of type X. X has a number of parameters and must be unique in the collection. (I created my own equals method based on the custom parameters to examine this)
In each object of type x, I have a list of objects y.
I want to add/remove/modify easily an object y.
For example:
To write the add method, it would be something like add(objTypeX, objTypeY)
I would check or the collections already has a objTypeX.
If so: i would add the objTypeY to the already existing objTypeX
else: i would create objTypeX and add objTypeY to this object.
To modify an objTypeY, it would be something like(objTypeX, objTypeY, newobjTypeY)
I would get objTypeX out of the collections and modify objTypeY to newobjTypeY
Which collections should I use? I tried with hashset but i can get a specific object out of the list, without run down the list till I find that object.
I develop this in vb.net 3.5
发布评论
评论(1)
为了高效查找,您应该使用考虑唯一参数的哈希值覆盖 GetHashCode();那么你可以使用
Dictionary>
(抱歉,不知道泛型的 VB 语法...),或者,如果 X 处理它自己的 Y 集合,只需使用HashSet
即可。For efficient lookup you should override GetHashCode() with a hash that takes the unique parameters into account; then you can either use a
Dictionary<X, IList<Y>>
(sorry, don't know the VB syntax for generics...) or, if X handles its own collection of Y, simply use aHashSet<X>
.