如何使用HashSet的< MyCustomClass>除去MyCustomClass的重复? [英] How can I use a HashSet<MyCustomClass> to remove duplicates of MyCustomClass?

查看:103
本文介绍了如何使用HashSet的< MyCustomClass>除去MyCustomClass的重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的HashSet< MyCustomClass> MYSET =新的HashSet< MyCustomClass>(); 我希望删除包含相同值的所有MyCustomClass的。

I have a HashSet<MyCustomClass> mySet = new HashSet<MyCustomClass>(); and I wish to remove all MyCustomClass's that contain the same values.

让我们说MyCustomClass看起来是这样的:

Let's say MyCustomClass looks like this:

public class MyCustomClass
{
    Point point;

    public MyCustomClass(int x, int y)
    {
        point.X = x;
        point.Y = y;
    }

    // Other methods...
}

我试图执行的IEqualityComparer 像MSDN建议,并通过它通过 HashSet的<的构造; MyCustomClass>(); 但是我最终没有成功。

I tried to implement IEqualityComparer like MSDN suggests, and pass it through the constructor of the HashSet<MyCustomClass>(); but I ended up unsuccessfully.

什么是正确的做法。

编辑:

这是我的类和我的 ChainEqualityComparer

public class Chain
{
    HashSet<Mark> chain;
    HashSet<Mark> marks;

    public Chain(HashSet<Mark> marks)
    {
        chain = new HashSet<Mark>();
        this.marks = marks;
    }       
    // Other methods...
}

public class ChainEqualityComparer : IEqualityComparer<Chain>
{
    #region IEqualityComparer<Chain> Members

    public bool Equals(Chain x, Chain y)
    {
        if (x.ChainWithMarks.Count == y.ChainWithMarks.Count)
        {
            foreach (Mark mark in x.ChainWithMarks)
            {
                if (!y.ChainWithMarks.Contains(mark))
                    return false;
            }
            return true;
        }

        return false;
    }

    public int GetHashCode(Chain obj)
    {
        return obj.GetHashCode() ^ obj.GetType().GetHashCode();
    }

    #endregion
}

和这是我的标记类:

  public class Mark
{
    int x;
    int y;

    public Mark(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public int X
    {
        get { return x; }
        set { x = value; }
    }

    public int Y
    {
        get { return y; }
        set { y = value; }
    }
}

public class MarkEqualityComparer : IEqualityComparer<Mark>
{
    #region IEqualityComparer<Mark> Members

    public bool Equals(Mark x, Mark y)
    {
        return (x.X == y.X) && (x.Y == y.Y);
    }

    public int GetHashCode(Mark obj)
    {
        return obj.GetHashCode() ^ obj.GetType().GetHashCode();
    }

    #endregion
}



(我可以引擎收录的代码,如果它是太多的代码......)

(I can pastebin the code if it's too much code...)

推荐答案

您可以使用EqualityComparer或只是重载Equals和GetHashCode

You can use the EqualityComparer or just override Equals and GetHashCode.

您必须确保,无论你认为是一个的重复的被确定为当测试具有等同的散列码,并返回true平等。

You must make sure that whatever you consider to be a duplicate is identified as having an equivalent hash code, and returning true when tested for equality.

我的猜测是,你不回同等的哈希码。 ?你可以从你的相等比较张贴代码

My guess is that you weren't returning equal hash codes. Could you post the code from your equality comparer?

作为一个测试,你可以这样做:

As a test, you could do:

var set = new HashSet<MyCustomClass>();
var a = new MyCustomClass(1,2);
var b = new MyCustomClass(1,2);
set.Add(a);
set.Add(b);
Assert.IsTrue(a.Equals(b));
Assert.IsTrue(b.Equals(a));
Assert.AreEqual(a.GetHashCode(), b.GetHashCode());
Assert.AreEqual(1, set.Count);

一个类似的一系列测试将适用于相等比较了。

A similar set of tests would be applicable to an equality comparer too.

修改

是的,涉嫌它的哈希码功能。你需要根据类型本身的价值来计算。一个常见足够的错误。

Yep, as suspected it's the hash code function. You need to calculate it based on the values of the type itself. A common enough mistake.

public int GetHashCode(Mark obj)
{
    return ((MyCustomClass)obj).point.GetHashCode();
}

这假定在你的类唯一的国家领域。

That assumes point is the only state field in your type.

这篇关于如何使用HashSet的&LT; MyCustomClass&GT;除去MyCustomClass的重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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