Hi every body.
Sorry in advance for any misstypes my english is not perfect.
I'm having a problem using the HashSet contains method.
For some reason the method is not working right.
I have a NewsChannel class that has a HashSet of TVProgram objects.
I've overwritten the equals and the hashcode methods.
When I check in the MainClass if two TVProgram objects are equal the output is true. and the hash code is the same. But after a add a program to the hashset and check if the same object exsits with the contains method the output is false.
This is the equal an the hashcode in the TVProgram Class:

Send the code ...Some confusion with ur flow or else it would have worked.

Send the code with code tag

09-01-2009, 10:42 AM

r035198x

You will need to show how put the p1 in news1.getPrograms() (if at all).

P.S Why not use instanceof test instead of class tests? That way subclasses of TVProgram can still use your equals.

09-01-2009, 01:45 PM

guywalder

I'm using the contains method in an addTVProgram method in the NewsChannel class. I need to check if a program is already in the HashSet each time I'm adding a new program so i wont have doubles or run over an existing program. So I cant use the instanceOf Method.
Ramya what part of the code you need? I added the parts that I think are relevant.
Still no solution...
Thanks 4 your help.

09-01-2009, 01:49 PM

r035198x

You can still use the instanceof to replace the parts that are doing getClass() comparisons. The rest of your equals method would stay the same.
Checking for contains in a HashSet is rather redundant. You can't have duplicates in a Set any way.
The problem is probably that you never added the TVProgram into the set in the first place that's why the contains is always returning false. Post that code that adds the TVProgram that you are checking against.

09-01-2009, 01:53 PM

RamyaSivakanth

Any security issues to send the code?

Please send the code so that it will help us to guide you.

09-01-2009, 02:23 PM

guywalder

1 Attachment(s)

I did add the program to the list and when I printed the news1 object the programs HashSet is printed as well including the program added.
This is the addProgram method from the NewsChannel Class:

Notice the capitalisation (which you had right inside the method, but not in the method declaration).

If you use the override annotation you won't make these types of mistakes.

09-01-2009, 02:32 PM

r035198x

The code you posted above doesn't even compile.

You also seem to have ignored the part of my post which said
"Checking for contains in a HashSet is rather redundant. You can't have duplicates in a Set any way."

09-01-2009, 02:42 PM

guywalder

I can't belive it.... :)
Thank you masijade you where right. I cant belive that i spent 4 hours just to find out that I wrote the Method hashCode with a small c and ther for did'nt over write it.
Thanx to every one how helped.
Guy.

09-01-2009, 02:44 PM

guywalder

r035198x you are right but you can over right an existing one.
Thank you for your help due.

09-01-2009, 02:48 PM

masijade

Quote:

Originally Posted by guywalder

r035198x you are right but you can over right an existing one.
Thank you for your help due.

If by this, you mean by calling add(Object) will overwrite (not over right) the existing "Object", if it exists. You're wrong. the add method returns a boolean, true if the Object is successfully added, false if the Object already exists and so cannot be added (which, of course, implies that it does not "overwrite" anything).