在编写 hashcode 和 equals 方法方面需要帮助吗?

发布于 2024-12-03 06:00:03 字数 244 浏览 2 评论 0原文

我有一个提供有关付款详细信息的类。属性在

    accountNo, transactionAmount, dateOfTransaction.

这里我想编写哈希函数,以便当我将此类对象存储在哈希集中时它会高效。

主要限制是付款详细信息应该是唯一的(假设某个人不应在一个月内支付两次费用)。

任何人都可以帮助我为这种情况编写 hashCode 以及 equals 方法吗?

I have a class that gives details about payment.The attributes are

    accountNo, transactionAmount, dateOfTransaction.

Here I want to write hash function such that it will efficient when I store this class objects in a hashSet.

The main constraint is payment details should be unique(suppose a particular person should not pay fee two times in a month).

Can any one help me in hashCode to be written for this scenario and also equals method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦年海沫深 2024-12-10 06:00:03

您需要准确地确定平等的含义。特别是,您谈到一个月内不支付两次 - 这是否意味着一笔交易应该与另一笔交易在同一个月内相等,即使是在不同的一天?这听起来很奇怪——而且是特定于用法而不是特定于类型的——平等的想法。另请注意,交易只有一个帐号 - 当然它应该有一个“发件人”帐户和一个“收件人”帐户,因为可能有多个人向同一帐户付款,并且可能有在同一个月内从一个帐户向多个帐户付款。

所以,就我个人而言,我不想以这种方式覆盖平等,但如果你真的必须这样做,这并不太难......一旦你决定了什么构成平等,我将实现equals - 那时 hashCode 通常相当简单。

强烈建议您阅读 Josh Bloch 在 中关于平等的部分《Effective Java》(第二版) 了解更多详细信息,但 equals 通常看起来像这样:

@Override public boolean equals(Object other)
{
    if (other == null || other.getClass() != this.getClass())
    {
        return false;
    }
    BankTransaction otherTransaction = (BankTransaction) other;

    return accountNo == otherTransaction.accountNo 
        && transactionAmount == otherTransaction.transactionAmount
        && // etc;
}

请注意,对于任何属于引用类型,您需要确定要在那里应用哪种相等性 - 通常您需要调用 equals 而不是仅使用 ==。

You need to decide exactly what you mean by equality. In particular, you talk about not paying twice in a month - does that mean one transaction should be equal to another if it's in the same month even if it's on a different day? That sounds like quite an odd - and very usage-specific rather than type-specific - idea of equality. Also note that the transaction only has one account number - surely it should have both a "from" and a "to" account, as there could be payments from multiple people to the same account, and there could be payments from one account to multiple accounts in the same month.

So, personally I wouldn't want to override equality in this way, but if you really do have to, it's not too hard... Once you've decided on what consitutes equality, I would implement equals - at that point hashCode is usually fairly easy.

I would strongly recommend that you read Josh Bloch's section on equality in Effective Java (second edition) for more details, but equals would typically look something like this:

@Override public boolean equals(Object other)
{
    if (other == null || other.getClass() != this.getClass())
    {
        return false;
    }
    BankTransaction otherTransaction = (BankTransaction) other;

    return accountNo == otherTransaction.accountNo 
        && transactionAmount == otherTransaction.transactionAmount
        && // etc;
}

Note that for any field which is a reference type, you need to determine what sort of equality you want to apply there - often you'll want to call equals instead of just using the reference comparison provided by ==.

待天淡蓝洁白时 2024-12-10 06:00:03

我建议您使用apache commons包的hashcodebuilder:

http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/builder/HashCodeBuilder

还有一个 EqualsBuilder:

http://commons.apache.org/lang/api -2.6/org/apache/commons/lang/builder/EqualsBuilder

如果你实现了这两个,你就不应该担心将对象存储在哈希集中

I suggest you use the hashcodebuilder of the apache commons package:

http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/builder/HashCodeBuilder

There is also an EqualsBuilder:

http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/builder/EqualsBuilder

If you implememt both you should not worry about storing your objects in a hashset

风情万种。 2024-12-10 06:00:03

这个问题的明确答案在Effective Java(第二版)中。

The definitive answer to this question is in Effective Java (second edition).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文