Problem with contains() method in ArrayList

C

Chanchal

hi,
In an application i'm developing, i have a class say X and
an arraylist of X ie,

list = new ArrayList<X>() ;

and i have added an object of X into list

list.add(x); // x ib object of X

and i'm calling the contains() function of the arraylist to check if an
object of X, say 'y' is in there.

I have overridden the equals() method in X and it returns true for
x.equals(y).

But the contains() method returns false. what could be the reason.
please advice

Chanchal
 
T

Torkel Franzen

Chanchal said:
I have overridden the equals() method in X and it returns true for
x.equals(y).

But the contains() method returns false. what could be the reason.

That you haven't in fact overridden equals.
 
C

Chanchal

Hi Franzen,
i haven't fully understood your comment that i haven't overridden
equals

this is my equals() function

public boolean equals(SaleItem saleItem){
return (saleItem instanceof Service &&
this.name.equals(saleItem.getName()));
}

am i missing anything here??

Chanchal
 
J

Jean-Francois Briere

The equals method MUST have the following signature:

public boolean equals(Object obj)

Regards
 
J

Jean-Francois Briere

this is my equals() function
public boolean equals(SaleItem saleItem){
return (saleItem instanceof Service &&
this.name.equals(saleItem.getName()));
}

Here you are overloading the equals method (same name, different
parameter number / type).
You are not overriding it (same name, same parameter number / type).

Regards
 
T

Thomas Hawtin

Jean-Francois Briere said:
The equals method MUST have the following signature:

public boolean equals(Object obj)

Adding @Override to methods that are supposed to override will cause the
compiler to catch these sorts of errors.

Tom Hawtin
 
C

Chanchal

hi thomas

thanks for that tip. but where should i add @Override? should the
signature of function be

@override public boolean equals(Object obj) ? where can i find similar
swithces??

Chanchal
 
C

Chanchal

hi thomas

thanks for that tip. but where should i add @Override? should the
signature of function be

@override public boolean equals(Object obj) ? where can i find similar
switches??

Chanchal
 
T

Thomas Hawtin

Jean-Francois Briere said:
The @Override annotation is not a part of the method signature.
It is just information to the compiler.
It's more like:

@Override
public boolean equals(Object obj)
{
...
}

For @Override only, some people will write that as:

@Override public boolean equals(Object obj)

Not sure how widespread that is. We still haven't got everyone to put
opening braces at end of line.

And the JavaDocs for @Override itself. Annotations are hidden right at
the bottom of the type lists for packages (far bottom left). Note, Java
is case sensitive, so always a capital O.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Override.html

Tom Hawtin
 
A

Adam Maass

Jean-Francois Briere said:
Here you are overloading the equals method (same name, different
parameter number / type).
You are not overriding it (same name, same parameter number / type).

IE, you need this (or something similar) instead (note that the type of the
parameter is Object, not SaleItem):

public boolean equals(Object other)
{
if(other instanceof SaleItem)
{
SaleItem saleItem = (SaleItem)other;
return this.name.equals(saleItem.name);
}
else
{
return false;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top