是HashSet的< T>相同的名单,其中,T>但与独特性? [英] Is a HashSet<T> the same as List<T> but with uniqueness?

查看:119
本文介绍了是HashSet的< T>相同的名单,其中,T>但与独特性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有一个集合的唯一项目的能力。

I need to have an ability to have unique items in a collection.

我会用字典,所以我可以用ContainsKey方法,但我认为这将是一个废物我不会使用键/值对的Value属性。

I was going to use a Dictionary so I could use the ContainsKey method but I thought it would be a waste as I wouldnt use the Value property of the Key/Value pair.

我遇到了的HashSet< T> 这看起来非常有前途。我唯一​​能找到我找不到在名单,其中,T> 文档是的HashSet< T> 是无序的。我觉得挺好的,我认为这意味着它使用的是的IEqualityComparer 没有下令。只要在对象被添加的顺序是相同的索引位置,我认为这将是确定,因为我必须做重复检查,因此HashSet中,然后检查所有条目顺序。

I came across the HashSet<T> which looks very promising. The only thing I can find that I can't find in the List<T> docs is that HashSet<T> is unordered. I think that is fine, I assume it means its not ordered using a IEqualityComparer. As long as the order in which items are added are in the same index position I think it will be ok as I have to do duplicate checking hence the hashset and then check all entries are sequential.

还有什么我已经错过了比较两种类型?

Is there anything else I have missed comparing the two types?

推荐答案

没有,重要的的HashSet&LT; T&GT; 没有排序或索引任何概念 - 列表在概念上有槽0 .... N-1,而一组是只是一组。

No, importantly HashSet<T> doesn't have any concept of ordering or indexing - a list conceptually has slots 0....n-1, whereas a set is "just a set".

我觉得挺好的,我认为这意味着使用的IEqualityComparer它没有下令。

I think that is fine, I assume it means its not ordered using a IEqualityComparer.

的IEqualityComparer 不用于反正订购 - 它的只有的有关平等和哈希codeS会谈。 的HashSet&LT; T&GT; T&GT; 或者的元素比较(如,比方说,的SortedSet&LT不下令code>是)的的插入顺序。

IEqualityComparer isn't used for ordering anyway - it only talks about equality and hash codes. HashSet<T> isn't ordered by either an element comparison (as, say, SortedSet<T> is) or insertion order.

只要在该项目的添加顺序都是相同的索引位置,我认为这将是确定。

As long as the order in which items are added are in the same index position I think it will be ok.

目前的的没有索引的位置,而当你遍历一个的HashSet&LT; T&GT; 也不能保证你会得到他们回来了为了在其中添加它们。如果你连的的有关订购,的HashSet&LT; T&GT; 不是你追求的是什么

There is no index position, and when you iterate over a HashSet<T> there's no guarantee you'll get them back in the order in which you added them. If you're even thinking about ordering, HashSet<T> isn't what you're after.

话说回来,这一切是的的真词典&LT; TKEY的,TValue&GT; - 你不应该让有关订购有任何假设,无论是。

Then again, all of this is also true of Dictionary<TKey, TValue> - you shouldn't make any assumptions about ordering there, either.

这篇关于是HashSet的&LT; T&GT;相同的名单,其中,T&GT;但与独特性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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