aspose file tools*
The moose likes Beginning Java and the fly likes null equals null? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "null equals null?" Watch "null equals null?" New topic
Author

null equals null?

Steve Jiang
Ranch Hand

Joined: May 17, 2004
Posts: 107
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

Joined: Aug 20, 2001
Posts: 1824

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 ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Steve Jiang
Ranch Hand

Joined: May 17, 2004
Posts: 107
I did it and it still insert the duplicate data. Comments?

Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

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

Joined: Aug 20, 2001
Posts: 1824

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

Joined: May 17, 2004
Posts: 107
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

Joined: Aug 20, 2001
Posts: 1824

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

Joined: Jul 11, 2001
Posts: 14112
As an aside

if (b) return true; else return false;

can be written shorter as

return b;


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null equals null?