Tag: hashset

从.NET HashSet中按索引选择元素

目前我正在使用从HashSet派生的自定义类。 在特定条件下选择项目时代码中有一点: var c = clusters.Where(x => x.Label != null && x.Label.Equals(someLabel)); 它工作正常,我得到了这些元素。 但有没有办法可以在集合中接收元素的索引以与ElementAt方法一起使用,而不是整个对象? 看起来或多或少会像这样: var c = select element index in collection under certain condition; int index = c.ElementAt(0); //get first index clusters.ElementAt(index).RunObjectMthod(); 是否手动迭代整个集合更好的方法? 我需要补充说它是一个更大的循环,所以这个Where子句对不同的someLabel字符串执行多次。 编辑 我需要这个吗? clusters是一组文档集合的集合。 文档按主题相似性分组。 因此,算法的最后一步是发现每个群集的标签。 但算法并不完美,有时它会使两个或多个具有相同标签的聚类。 我想要做的只是将这些集群合并为一个集群。

在HashSet 中包含线程安全

查看.NET源代码中HashSet类中Contains的代码,我找不到Contains不是线程安全的原因吗? 我正在提前使用值加载HashSet ,然后在multithreading中检查Contains 。 AsParallel()循环。 这有什么理由不安全吗? 当我实际上不需要存储值时,我不愿意使用ConcurrentDictionary 。

为什么有HashSet但没有在C#中设置?

老问题 我的理解是C#在某种意义上具有HashSet和set类型。 我理解HashSet是什么。 但为什么set是一个单独的词? 为什么不是每个集合都是HashSet ? 新问题 为什么C#没有genericsSet类型,类似于Dictionary类型? 从我的角度来看,我希望有一个具有标准查找/添加/删除性能的集合。 我不关心它是用哈希还是别的来实现的。 那么为什么不在这个版本的C#中创建一个实际上被实现为HashSet的集合类,但在将来的版本中可能会有所不同? 或者为什么不至少接口ISet ? 回答 感谢所有回答如下的人: ICollection实现了很多你对ISet期望。 但是,从我的观点来看, ICollection实现了IEnumerable而集合不必是可枚举的—例如:1到2之间的实数集合(甚至更多,集合可以动态生成)。 我同意这是一个小咆哮,因为“普通程序员”很少需要不可数集。 好吧,我想我明白了。 HashSet绝对意味着被称为Set但在某种意义上保留了Set这个词。 更具体地说,.NET体系结构的创建者希望为不同的语言提供一致的集合(sic!)。 这意味着标准类的每个名称都不得与.NET语言中的任何关键字一致。 然而,在VB.NET中使用了这个词,它实际上是不区分大小写的(是吗?),所以不幸的是那里没有空间。 谜团已揭开 :) 结语 Alex Y的新答案链接到MSDN页面 ,该页面描述了即将推出的.NET 4.0接口ISet ,它的行为与我认为应该HashedSet并由HashedSet实现。 好结局。

序列化HashSet

我正在尝试序列化Hashset,但我没有运气。 每当我尝试打开序列化数据时,我都会得到一个空的HashSet。 但是,List工作正常。 示例代码: [Serializable()] public class MyClass : ISerializable { public MyClass(SerializationInfo info, StreamingContext ctxt) { HashSet hashset = (HashSet)info.GetValue(“hashset”, typeof(HashSet)); List list = (List)info.GetValue(“list”, typeof(List)); Console.WriteLine(“Printing Hashset:”); foreach (string line in hashset) { Console.WriteLine(line); } Console.WriteLine(“Printing List:”); foreach (string line in list) { Console.WriteLine(line); } } public void GetObjectData(SerializationInfo info, StreamingContext ctxt) { […]

C#Hashset包含非唯一对象

使用这个类 public class Foo { public string c1, c2; public Foo(string one, string two) { c1 = one; c2 = two; } public override int GetHashCode() { return (c1 + c2).GetHashCode(); } } 而这个HashSet HashSet aFoos = new HashSet(); Foo aFoo = new Foo(“a”, “b”); aFoos.Add(aFoo); aFoos.Add(new Foo(“a”, “b”)); label1.Text = aFoos.Count().ToString(); 我得到答案2,肯定它应该是1.有没有办法解决这个问题,所以我的HashSet只包含唯一的对象? 谢谢,阿什。

如何将Comparer用于HashSet

由于我在这里问的另一个问题,我想为我的对象使用HashSet 我将创建包含字符串的对象和对其所有者的引用。 public class Synonym { private string name; private Stock owner; public Stock(string NameSynonym, Stock stock) { name=NameSynonym; owner=stock } // [+ ‘get’ for ‘name’ and ‘owner’] } 我知道我需要一个比较器,但以前从未使用它。 我应该创建一个单独的类吗? 喜欢: public class SynonymComparer : IComparer { public int Compare(Synonym One, Synonym Two) { // Should I test if ‘One == null’ or ‘Two […]

如何在不枚举的情况下访问HashSet 的引用值?

我有这种情况,其中内存保护是至关重要的。 我试图将> 1 GB的肽序列读入共享相同序列的记忆和组肽实例中。 我将Peptide对象存储在Hash中,因此我可以快速检查重复,但发现即使知道Set包含该对象,也无法访问Set中的对象。 内存非常重要,如果可能,我不想复制数据。 (否则我会将我的数据结构设计为:peptides = Dictionary但这会复制字典和Peptide类中的字符串)。 下面是代码,向您展示我想要完成的任务: public SomeClass { // Main Storage of all the Peptide instances, class provided below private HashSet peptides = new HashSet(); public void SomeMethod(IEnumerable files) { foreach(string file in files) { using(PeptideReader reader = new PeptideReader(file)) { foreach(DataLine line in reader.ReadNextLine()) { Peptide testPep = new […]

如何在.Net中实现ConcurrentHashSet

我试图在ConcurrentDictionary的精神中实现一个ConcurrentHashSet,采取的方法是使用内部支持ConcurrentDictionary并编写小的委托方法,这是我得到了多远,但是设置理论方法是我坚持,尤其是。 我不确定我是否可以使用foreach并且仍然不违反并发性 public class ConcurrentHashSet : ISet { private readonly ConcurrentDictionary _internal; public ConcurrentHashSet(IEnumerable elements = null) { _internal = new ConcurrentDictionary(); if (elements != null) UnionWith(elements); } public void UnionWith(IEnumerable other) { if (other == null) throw new ArgumentNullException(“other”); foreach (var otherElement in other) Add(otherElement); } public void IntersectWith(IEnumerable other) { throw new NotImplementedException(); […]