简体   繁体   English

如何制作HashSet <HashSet<int> &gt;工作正常吗?

[英]How to make a HashSet<HashSet<int>> work appropriately?

I have a program where I use an algorithm to determine the 'indices' of an object to determine if it's a unique object, or if it is really holding the same data as another similar object. 我有一个程序,其中使用算法确定对象的“索引”,以确定该对象是否是唯一对象,或者它是否确实与另一个相似对象拥有相同的数据。

The problem is that because of the way this object was created, I can't rely on these indices being in the same order. 问题在于,由于创建该对象的方式不同,我不能依赖这些索引的顺序相同。

For example, an object with indices: 例如,具有索引的对象:

[0, 1, 2] is the same as the object as [0, 2, 1] . [0, 1, 2][0, 2, 1]的对象相同。

To handle this problem, I've been putting these indices into a HashSet<int> , in the hopes that when I go to compare the sets in the future, it will be easy because in set notation, {0, 1, 2} is the same as {0, 2, 1} . 为了解决这个问题,我一直将这些索引放入HashSet<int>中,希望以后在比较集合时会很容易,因为在集合符号{0, 1, 2}{0, 2, 1}

The thing is, I can't just go 事情是,我不能走

Object1.UniqueHashSet.equals(Object2.UniqueHashSet) 

because I have A LOT of these objects to compare, and I need to determine how many unique objects I have in a list of these. 因为我有很多要比较的对象,所以我需要确定这些列表中有多少个唯一的对象。

To get around this, I've been trying to add these structures into a HashSet<HashSet<int>> of themselves, so that in the end I can just return the number of elements in the HashSet. 为了解决这个问题,我一直试图将这些结构添加到自身的HashSet<HashSet<int>>中,以便最后我可以只返回HashSet中的元素数。

When I go to add the HashSet<int> into the HashSet<HashSet<int>> with values {0, 1, 2} , and then later add the HashSet<int> of {0, 2, 1} into the HashSet<HashSet<int>> it creates two new elements in the set, when I really only wanted one. 当我去将HashSet<int>添加到具有值{0, 1, 2}HashSet<HashSet<int>> ,然后稍后将{0, 2, 1} HashSet<int> {0, 1, 2}HashSet<int>添加到HashSet<HashSet<int>>当我真的只想要一个元素时,它会在集合中创建两个新元素。 So it turns out I have: {{0, 2, 1}, {0, 1, 2}} , when I really wanted {{0, 2, 1}} . 原来我有: {{0, 2, 1}, {0, 1, 2}} ,当我真正想要{{0, 2, 1}}

I've been racking my brain against the wall for hours trying to figure out how to do this. 数小时以来,我一直在脑袋里摸索着想办法。 Can someone please point me in the right direction? 有人可以指出正确的方向吗?

You'll want to create an inherited class of HashSet. 您将要创建HashSet的继承类。 You'll want to override the following methods: 您将要覆盖以下方法:

public override int GetHashCode() { /* Implementation */ }
public override bool Equals(object obj) { /* Implementation */ }

The properties will be based on the members of the set. 这些属性将基于集合的成员。

Then, inserting into the HashSet of the custom class will not lead to duplicates. 然后,插入到自定义类的HashSet中不会导致重复。

You'll have a structure like: 您将拥有一个类似的结构:

HashSet<MyHashSet<int>>

The GetHashCode should be a computation based on the individual values in the set. GetHashCode应该是基于集合中各个值的计算。 The Equals should be checking for equals for the members of the set. 等于应检查集合成员的等于。

Here's some more information on the topic: 以下是有关该主题的更多信息:

How should I override Equals and GetHashCode for HashSet? 如何为HashSet覆盖Equals和GetHashCode?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM