File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Date and override equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Date and override equals()" Watch "Date and override equals()" New topic
Author

Date and override equals()

rick collette
Ranch Hand

Joined: Mar 22, 2002
Posts: 208
Hi,
I have a class like the following:
import java.util.Date;
Class A {
Date date1;
String st;
public A (...){
...}
public boolean equals(Object o){
A other = (A)o;
boolean retval = false;
if(this == other) return true;
if(other instanceof A){
retval = retval && other.date1.equals(this.date1);
retval = retval && other.st.equals(this.st);
}
return retval;
}
public int hashCode(){
int result = 17;
result = 37*result + (int)(date1.getTime());
resulr = st == null ? result : 37 * result + st.hasCode();
}
}

When I try to compare two objects which are the same, it always tells me
they have different hashCode. I think something is wrong with equals()
method, just do not why it is not working.
Could anyone point out my mistakes?
regards,
[ August 28, 2003: Message edited by: rick collette ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Well, looking at this method:

  • Note #1: This cast will fail if o isn't an instance of A.
  • Note #2: Since other is an A, this test is redundant.
  • Note #3, retval will always be false on entering this statement,

  • so it will always be false after evaluating it
  • Note #4: Same as #3.


  • So this method always returns false. I think you might be looking for something like

    [ August 28, 2003: Message edited by: Ernest Friedman-Hill ]

    [Jess in Action][AskingGoodQuestions]
    rick collette
    Ranch Hand

    Joined: Mar 22, 2002
    Posts: 208
    Thanks. Sorry for having many senseless mistakes.
    I used your code, but I still got the same error. I wonder if the way
    I handle Date objects is wrong. Any inputs?
    regards

    Originally posted by Ernest Friedman-Hill:
    Well, looking at this method:

  • Note #1: This cast will fail if o isn't an instance of A.
  • Note #2: Since other is an A, this test is redundant.
  • Note #3, retval will always be false on entering this statement,

  • so it will always be false after evaluating it
  • Note #4: Same as #3.


  • So this method always returns false. I think you might be looking for something like

    [ August 28, 2003: Message edited by: Ernest Friedman-Hill ]
    Ernest Friedman-Hill
    author and iconoclast
    Marshal

    Joined: Jul 08, 2003
    Posts: 24187
        
      34

    Well, given that the typos were corrected (the hashCode() method you've shown spells "result" as "resulr" in one place, and hashCode() as hasCode() in another) it looks to me as if two objects for which my equals() method returns true should have the same hashCode() as well.
    Maybe you could show us some code where you create A objects and make these comparisons, and tell us what the result is? Try to use real cut-and-pasted code rather than typed-in code, because sometimes typos can be important!
    rick collette
    Ranch Hand

    Joined: Mar 22, 2002
    Posts: 208
    Thanks,
    I actually save the A object to the database first, then
    use its primary key to retrieve this object, and store it
    in another variable. Then I compare them.
    If I remove Date variables from my class, and use Strings,
    then everything runs fine. If I use Date varibales, then I
    got not equal error. I guess it is possible I am getting different
    Date values from database. I will check.
    regards,
    Originally posted by Ernest Friedman-Hill:
    Well, given that the typos were corrected (the hashCode() method you've shown spells "result" as "resulr" in one place, and hashCode() as hasCode() in another) it looks to me as if two objects for which my equals() method returns true should have the same hashCode() as well.
    Maybe you could show us some code where you create A objects and make these comparisons, and tell us what the result is? Try to use real cut-and-pasted code rather than typed-in code, because sometimes typos can be important!
    Jamie Robertson
    Ranch Hand

    Joined: Jul 09, 2001
    Posts: 1879

    just another note, the Date's must be equal to the millisecond. So if at any point, your precision drops below milliseconds, then the dates will not be equal. ( ie. on the insertion into/retrieval from the database )
    Jamie
    Dana Hanna
    Ranch Hand

    Joined: Feb 28, 2003
    Posts: 227
    Add a toString() method to print out the values.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Date and override equals()