C#集合集合?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/183685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:59:35  来源:igfitidea点击:

C# Set collection?

c#.netcollectionsset

提问by Omar Kooheji

Does anyone know if there is a good equivalent to Java's Setcollection in C#? I know that you can somewhat mimic a set using a Dictionaryor a HashTableby populating but ignoring the values, but that's not a very elegant way.

有谁知道Set在 C# 中是否有一个很好的相当于 Java 的集合?我知道您可以通过填充但忽略值来使用 aDictionary或 a模拟集合HashTable,但这不是一种非常优雅的方式。

采纳答案by Leahn Novash

Try HashSet:

尝试HashSet

The HashSet(Of?T) class provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order...

The capacity of a HashSet(Of?T) object is the number of elements that the object can hold. A HashSet(Of?T) object's capacity automatically increases as elements are added to the object.

The HashSet(Of?T) class is based on the model of mathematical sets and provides high-performance set operations similar to accessing the keys of the Dictionary(Of?TKey,?TValue)or Hashtablecollections. In simple terms, the HashSet(Of?T) class can be thought of as a Dictionary(Of?TKey,?TValue)collection without values.

A HashSet(Of?T) collection is not sorted and cannot contain duplicate elements...

HashSet(Of?T) 类提供高性能的集合操作。集合是一个不包含重复元素的集合,其元素没有特定的顺序......

HashSet(Of?T) 对象的容量是该对象可以容纳的元素数。HashSet(Of?T) 对象的容量会随着元素添加到对象中而自动增加。

HashSet(Of?T) 类基于数学集合模型,提供类似于访问Dictionary(Of?TKey,?TValue)Hashtable集合的键的高性能集合操作。简单来说,HashSet(Of?T) 类可以被认为是一个没有值的Dictionary(Of?TKey,?TValue)集合。

HashSet(Of?T) 集合未排序且不能包含重复元素...

回答by Jon Skeet

If you're using .NET 3.5, you can use HashSet<T>. It's true that .NET doesn't cater for sets as well as Java does though.

如果您使用的是 .NET 3.5,则可以使用HashSet<T>. 的确,.NET 不像 Java 那样适合集合。

The Wintellect PowerCollectionsmay help too.

Wintellect的PowerCollections可以帮助太大。

回答by Chris Canal

I use Iesi.Collections http://www.codeproject.com/KB/recipes/sets.aspx

我使用 Iesi.Collections http://www.codeproject.com/KB/recipes/sets.aspx

It's used in lot of OSS projects, I first came across it in NHibernate

很多OSS项目都用到了,我第一次在NHibernate中遇到

回答by dpan

Have a look at PowerCollectionsover at CodePlex. Apart from Set and OrderedSet it has a few other usefull collection types such as Deque, MultiDictionary, Bag, OrderedBag, OrderedDictionary and OrderedMultiDictionary.

在 CodePlex 上查看PowerCollections。除了 Set 和 OrderedSet,它还有一些其他有用的集合类型,例如 Deque、MultiDictionary、Bag、OrderedBag、OrderedDictionary 和 OrderedMultiDictionary。

For more collections, there is also the C5 Generic Collection Library.

对于更多集合,还有C5 通用集合库

回答by thecoop

I use a wrapper around a Dictionary<T, object>, storing nulls in the values. This gives O(1) add, lookup and remove on the keys, and to all intents and purposes acts like a set.

我在 a 周围使用了一个包装器Dictionary<T, object>,将空值存储在值中。这使得 O(1) 在键上添加、查找和删除,并且对于所有意图和目的来说就像一个集合。

回答by Bob Heck

I know this is an old thread, but I was running into the same problem and found HashSet to be very unreliable because given the same seed, GetHashCode() returned different codes. So, I thought, why not just use a List and hide the add method like this

我知道这是一个旧线程,但我遇到了同样的问题,发现 HashSet 非常不可靠,因为给定相同的种子,GetHashCode() 返回不同的代码。所以,我想,为什么不直接使用 List 并像这样隐藏 add 方法

public class UniqueList<T> : List<T>
{
    public new void Add(T obj)
    {
        if(!Contains(obj))
        {
            base.Add(obj);
        }
    }
}

Because List uses the Equals method solely to determine equality, you can define the Equals method on your T type to make sure you get the desired results.

由于 List 仅使用 Equals 方法来确定相等性,因此您可以在 T 类型上定义 Equals 方法以确保获得所需的结果。

回答by Derek W

If you're using .NET 4.0 or later:

如果您使用的是 .NET 4.0 或更高版本:

In the case where you need sorting then use SortedSet<T>. Otherwise if you don't, then use HashSet<T>since it's O(1)for search and manipulate operations. Whereas SortedSet<T>is O(log n)for search and manipulate operations.

在需要排序的情况下,请使用SortedSet<T>. 否则,如果不这样做,则使用HashSet<T>它,因为它O(1)用于搜索和操作操作。虽然SortedSet<T>O(log n)搜索和处理操作。