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?

kamaci
  • 72,915
  • 69
  • 228
  • 366
satheesh
  • 1,443
  • 7
  • 28
  • 41
  • You might find Apache Commons or Guava useful: http://stackoverflow.com/questions/5038204/apache-commons-equals-hashcode-builder – denis.solonenko Sep 06 '11 at 06: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 06 '11 at 06:25
  • @amal this is an assignment given to me – satheesh Sep 06 '11 at 06: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 06 '11 at 06:31
  • @amal i understood what you are telling...but here i am writing just a java program with out any db connected.. – satheesh Sep 06 '11 at 06:33
  • so , just do a validation before you add to the actual collection . If a payment has already been entered for the account number in that particular month , you dont add it to the collection , but display a message instead. – amal Sep 06 '11 at 06:35
  • also , the link provided in the answers below is a must-read for a java programmer. So after the assignment , maybe you should check that out to :) . http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf – amal Sep 06 '11 at 06:39
  • @but i dont have that book with me i will try to get....if possible can u send me this part of that information given in that book.. – satheesh Sep 06 '11 at 06:42

3 Answers3

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 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 06 '11 at 06: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 06 '11 at 06:17
  • ...this is my previous question that relates to this – satheesh Sep 06 '11 at 06:19
  • http://stackoverflow.com/questions/7311934/which-collection-should-be-used-in-this-scenario – satheesh Sep 06 '11 at 06:20
  • @satheesh: That was before you specified that a "duplicate" would only be "in the same month" which doesn't sound much like a duplicate to me. – Jon Skeet Sep 06 '11 at 06:23
  • sorry by mistake i gave in that way – satheesh Sep 06 '11 at 06:25
  • 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 06 '11 at 06: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

leifg
  • 8,668
  • 13
  • 53
  • 79
2

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

Daniel
  • 10,115
  • 3
  • 44
  • 62
  • @Dabiel can u send me particular part of book that relates to this question..as i dont have this book – satheesh Sep 06 '11 at 06:36