0

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?

8
  • You might find Apache Commons or Guava useful: stackoverflow.com/questions/5038204/… Sep 6, 2011 at 6:09
  • @satheesh Are you sure the transaction class doesnt need a primary key ? And also , having read through your previous question - what is this ? An assignment or an app ? Are you persisting the data somewhere ?
    – amal
    Sep 6, 2011 at 6:25
  • @amal this is an assignment given to me
    – satheesh
    Sep 6, 2011 at 6:26
  • 1
    @satheesh are you persisting the data somewhere ? like a db ? And as mentioned in the below comments , I think you just need to validate the data before inserting into your collection/table . Overriding equals doesnt seem correct .As for the question in the title bar goes , you'll need to know what a hashcode is . Once you know that , implementing one would be trivial . I' not sure that's the way you should go to solve this particular problem however .
    – amal
    Sep 6, 2011 at 6:31
  • @amal i understood what you are telling...but here i am writing just a java program with out any db connected..
    – satheesh
    Sep 6, 2011 at 6:33

3 Answers 3

3

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 ==.

7
  • here student needs to deposit account every month.if he tries to deposit again for the same month it should reject.that means he should not pay two times for a particular purpose.
    – satheesh
    Sep 6, 2011 at 6:14
  • @satheesh: But that doesn't really sound like the transactions are "equal" - I don't think I'd override equals and hashCode for that purpose, to be honest.
    – Jon Skeet
    Sep 6, 2011 at 6:17
  • ...this is my previous question that relates to this
    – satheesh
    Sep 6, 2011 at 6:19
  • 2
    @satheesh: My point is that the answer you received before is quite possibly invalidated by the extra detail. I just don't think it's appropriate to override equals in a way that will actually get you the result you need here. You could do it and it would work - but as soon you need to consider transactions as equal in any other way, you'd be stuck. It's simply not a "natural" form of equality.
    – Jon Skeet
    Sep 6, 2011 at 6:27
3

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

3
  • @leigf, +1, I wanted to say that. Sep 6, 2011 at 7:05
  • 1
    @satheesh, Google is your friend. Tutorial on HashCodeBuilder. Sep 6, 2011 at 7:06
  • the link i provided has an example
    – leifg
    Sep 6, 2011 at 13:49
2

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

1
  • @Dabiel can u send me particular part of book that relates to this question..as i dont have this book
    – satheesh
    Sep 6, 2011 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.