Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null equals null?

 
Steve Jiang
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
classA a1 { m1 = null, m2 = "A" } classA a2{ m1= null, m2="A"}
Arraylist list = new ArrayList();
list.add(a1)
if ((list.contains(a2))) {
list.add(a2);

}



it seems the duplicated "A" was added in the list. I hope to add it in list only when m2 has diff value with A.

a1.equals(a2)???
I think null is OK as (e1==null ? e2==null : e1.equals(e2)).)
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what the intent here is, since the code provided will not compile. However, it looks like you are adding arrays into a List. As far as I know, .equals() for arrays is the same as ==; it compares if the physical objects are the same.



So even though the arrays have the same content, the .contains() fails because the arrays are not ==.

What you would need to do is write a class that wraps around an array and override .equals() on that class to interate through the array and see if the elements of the array are equal.
[ May 16, 2005: Message edited by: Joel McNary ]
 
Steve Jiang
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did it and it still insert the duplicate data. Comments?

 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a problem in your .equals method. The List class that contains the ".contains" code does not know about the .equals(Obj) method; it only knows about .equals(Object). Thus, you have to implement .equals(Object). What you did was you overloaded the method .equals. Overloaded methods do not participate in polymorphism. However, overridden methods do. To get this to work, change your .equals method to the following:

 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, note that you should always override .hashCode() when overriding .equals() (See Item 8 of Chapter 3 of Joshua Bloch's Effective Java
[ May 16, 2005: Message edited by: Joel McNary ]
 
Steve Jiang
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel,

Thanks for your comments.

someone argus that equals or hashcode should not be overwroten in java bean. But I really doubt it. Is there limition to overwrite equals() or hashcode() of Java beans?
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There really is no limitation, except to say that when you override .equals(), you should also override .hashCode(), otherwise you will get unexpected results. However, in many cases object identity (==) is what we want to test, so the default implementation of .equals() and .hashCode() will do fine. Only override .equals() if it provides meaning to your code.

As far as JavaBeans go, multiple Beans can have the same settings. If your beans have a key field, you might want to override .equals() to check on the key field, but otherwise the default functionality should work fine.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an aside

if (b) return true; else return false;

can be written shorter as

return b;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic