什么时候应该使用HashSet的< T>类型?什么时候、类型、LT、HashSet

2023-09-02 10:24:17 作者:ご沉默菋噵ご

我探索的HashSet< T> 的类型,但我不明白的地方矗立在集合

I am exploring the HashSet<T> type, but I don't understand where it stands in collections.

你可以使用它来替换名单,其中,T&GT; ?我想象的性能的HashSet&LT; T&GT; 会更好,但我看不出个人访问它的元素

Can one use it to replace a List<T>? I imagine the performance of a HashSet<T> to be better, but I couldn't see individual access to its elements.

难道只为枚举?

推荐答案

有关的重要的事情的HashSet&LT; T&GT; 是正确的,在这个名字:这是一个的设置的。唯一的东西,你可以用一组做的是确定哪些其成员,并检查项目是否是一个成员。

The important thing about HashSet<T> is right there in the name: it's a set. The only things you can do with a single set is to establish what its members are, and to check whether an item is a member.

询问是否可以检索一个元素(如设置[45] )被误解集的概念。有没有这样的事情为一组的45元。在一组项目没有顺序。在集{1,2,3}和{2,3,1}是在各方面都相同的,因为它们具有相同的成员资格,并籍是最重要的。

Asking if you can retrieve a single element (e.g. set[45]) is misunderstanding the concept of the set. There's no such thing as the 45th element of a set. Items in a set have no ordering. The sets {1, 2, 3} and {2, 3, 1} are identical in every respect because they have the same membership, and membership is all that matters.

这有点危险的迭代一个的HashSet&LT; T&GT; ,因为这样做强加在集中的项目订单。该顺序是不是真正的组的一个属性。你不应该依赖于它。如果在集合中的项目顺序对你很重要,该集合不是一套。

It's somewhat dangerous to iterate over a HashSet<T> because doing so imposes an order on the items in the set. That order is not really a property of the set. You should not rely on it. If ordering of the items in a collection is important to you, that collection isn't a set.

设定实在有限,并具有独特的成员。而另一方面,他们是真快。

Sets are really limited and with unique members. On the other hand, they're really fast.