File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date and override equals()

 
rick collette
Ranch Hand
Posts: 208
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
     
    rick collette
    Ranch Hand
    Posts: 208
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 24204
    34
    Chrome Eclipse IDE Mac OS X
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 208
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 1879
    MySQL Database Suse
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 227
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Add a toString() method to print out the values.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic