如何从HashSet的&LT实际的项目; T>? [英] How to retrieve actual item from HashSet<T>?

查看:140
本文介绍了如何从HashSet的&LT实际的项目; T>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过<一个href="http://stackoverflow.com/questions/1494812/why-cant-i-retrieve-an-item-from-a-hashset-without-enumeration">this为什么这是不可能的,但还没有找到一个解决问题的方法问题。

我想从.NET HashSet的&LT检索项目; T&GT; 。我在寻找,将这个签名的方法:

  ///&LT;总结&gt;
///确定此集包含的项目等于到&lt; paramref名称=项/&gt;中
///根据创建组时所使用的比较机制。
///集合不改变。如果集合确实包含一个项目等于
///&LT; paramref名称=项/&gt ;,然后从设定的项目时返回。
///&LT; /总结&gt;
布尔TryGetItem&LT; T&GT;(T项目,出牛逼foundItem);
 

搜索的集合为一个项目用这样的方法将是O(1)。只有这样,才能检索邮件后的的HashSet&LT; T&GT; 是枚举所有项目这是O(n)

我还没有找到任何解决此问题的其他然后让我自己的HashSet&LT; T&GT; 或使用词典&LT; K,V&GT; 。任何其他的想法?

注意:
我不想检查的HashSet&LT; T&GT; 包含的项目。我想要得到的引用存储在 HashSet的&LT的项目; T&GT; ,因为我需要更新(不通过另一个实例替换它)。我想传递给 TryGetItem 将(据我已经传递给构造的比较机制),但它不会是相同的参考是平等的。

解决方案

这其实是一个巨大的疏漏,在集藏品。就需要只键的任一个字典或HashSet的,允许对象引用的检索。因此,许多人都问,为什么它没有得到固定的我是无法理解。

如果没有第三方库的最好的解决办法是使用词典&LT; T,T&GT; 与键相同的值,因为字典存储它的哈希表项​​。性能方面是一样的HashSet的,但它浪费当然存储器(每个条目的指针的大小)

 词典&LT; T,T&GT; myHashedCollection;
...
如果(myHashedCollection.ContainsKey [项目])
    项目= myHashedCollection [项目] //代替重复
其他
    myHashedCollection.Add(项目,项目); //添加previously未知项目
...
//具有独特的项目工作
 

I've read this question about why it is not possible, but haven't found a solution to the problem.

I would like to retrieve an item from a .NET HashSet<T>. I'm looking for a method that would have this signature:

/// <summary>
/// Determines if this set contains an item equal to <paramref name="item"/>, 
/// according to the comparison mechanism that was used when the set was created. 
/// The set is not changed. If the set does contain an item equal to 
/// <paramref name="item"/>, then the item from the set is returned.
/// </summary>
bool TryGetItem<T>(T item, out T foundItem);

Searching the set for an item with such a method would be O(1). The only way to retrieve an item from a HashSet<T> is to enumerate all items which is O(n).

I haven't find any workaround to this problem other then making my own HashSet<T> or use a Dictionary<K, V>. Any other idea?

Note:
I don't want to check if the HashSet<T> contains the item. I want to get the reference to the item that is stored in the HashSet<T> because I need to update it (without replacing it by another instance). The item I would pass to the TryGetItem would be equal (according to the comparison mechanism that I've passed to the constructor) but it would not be the same reference.

解决方案

This is actually a huge omission in the set of collections. You would need either a Dictionary of keys only or a HashSet that allows for the retrieval of object references. So many people have asked for it, why it doesn't get fixed is beyond me.

Without third-party libraries the best workaround is to use Dictionary<T, T> with keys identical to values, since Dictionary stores its entries as a hash table. Performance-wise it is the same as the HashSet, but it wastes memory of course (size of a pointer per entry).

Dictionary<T, T> myHashedCollection;
...
if(myHashedCollection.ContainsKey[item])
    item = myHashedCollection[item]; //replace duplicate
else
    myHashedCollection.Add(item, item); //add previously unknown item
...
//work with unique item

这篇关于如何从HashSet的&LT实际的项目; T&GT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆