aspose file tools*
The moose likes Java in General and the fly likes List's contains(Object o) method doesn't work for object version. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "List Watch "List New topic
Author

List's contains(Object o) method doesn't work for object version.

Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
I have an ArrayList with the type of my own class. Every time I have try to add objects into that list, it has to validate whether that list already have that object or not with the sense of its members( exactly mean that, the list should accepted for different member). If already there then I have to show some error message. But while I'm trying to add the same object again, it hasn't validate. I have found out that the object already in that list has different version number from object which I'm going to inserted newly. That's why the list hasn't validate. Please Find code following.



Code for validation method


Code for MyClass


In that validation method, the actual object present in the list has version like "MyPack.MyClass@4293ef"
But at the second time I have tried to insert the MyClass object into list, it has version like "MyPack.MyClass@1fa5453"

Primary Need of validation method is It has to check whether that MyClass object with the member(say for example myName="Bharath", herName="UnKnown") is already in list or not. If its already there it has to return my error message.

Any suggestion.

Please Note : List is needed here. I'm using that list in many other classes. So I'm not ready to change it as "Set" as whole project.


Life is either daring something or nothing - Helen Keller
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Can you go to the List#contains API, read it, and see which method is missing in MyClass ?


[My Blog]
All roads lead to JavaRanch
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
Christophe Verré wrote:Can you go to the List#contains API, read it, and see which method is missing in MyClass ?


yes, chris... I have red it. But unable to know whats wrong in my "MyClass"... Can you please... But anyway as I've said my actual need is list has to accept different members("myName", "herName") only. Just now I've studied about 'comparator' method, and right now I'm trying to override it.. Hope that 'll clear my problem. But please advice me where I'm wrong in my "MyClass"
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

You must have missed that part :
More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).


Ok, it's not obvious But if your class does not override Object#equals, your objects will never be equal. So what do you have to do ?
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
Christophe Verré wrote:You must have missed that part :
More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

Ok, it's not obvious


yes.. I've not caught that point

Christophe Verré wrote:
But if your class does not override Object#equals, your objects will never be equal.

yes. I'm agree...

Christophe Verré wrote:
So what do you have to do ?


First thing is, if the list has object as "MyClass (with the member myName="he" herName="she"), and at the second time I'm trying add the same object with the same member it has to return error message. Just now I got info that 'java.util.Comparator" interface's "compare() " method will check the object including its members... Is that right... I'm trying to implement that now.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

If you consider that two instances of your class are equals when myName and herName are both equal, all you need it to override the equals() method and check that both fields are equals. No need to use any comparator in that case.
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
oh.. okay.. Let me try
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
Christophe Verré wrote:If you consider that two instances of your class are equals when myName and herName are both equal, all you need it to override the equals() method and check that both fields are equals. No need to use any comparator in that case.


yes chris.. thanks for your advice... equals method says ,


Override the equals method when you want to specify the rules of logical equality of objects. What is logical equality? A simple definition is that two objects are logically equal if they have the same values for the same uniqueness attributes.


above statement is find from bytes.com
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

So, did you come up with a suitable equals() method ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39882
    
  28
. . . and did you override hashCode()?
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
Christophe Verré wrote:So, did you come up with a suitable equals() method ?


yes chris... I have write suitable equals() method..
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
Campbell Ritchie wrote: . . . and did you override hashCode()?


yes ritchie.. I have overrides the hashCode() method also.. and oops.. got problem as returning different hash code for same object with same member values... I'm working on it now..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39882
    
  28
Bharath Raja wrote: . . . got problem as returning different hash code for same object with same member values . . .
Everybody thinks overriding hashCode is easy, but it isn't. Then they try overriding equals() and find it is even more difficult than overriding hashCode

Do a search of this forum and "beginning Java" and you should find some helpful pointers.
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
Campbell Ritchie wrote: Everybody thinks overriding hashCode is easy, but it isn't. Then they try overriding equals() and find it is even more difficult than overriding hashCode


yes.. I agree... In this whole day I'm struggling with this issue..
Campbell Ritchie wrote:
Do a search of this forum and "beginning Java" and you should find some helpful pointers.

sure.. now i'm searching...
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
I'm wake up this topic now. Hope all ranchers will help me especially chris and ritchie

calling overrides equals method



implementing overrides equals method


overriding hashCode() method


Now., the problem is whenever I'm executing my code, my overrided equals() method not getting called. Instead of that default Object classes equals() method only called. Can any one find out where I'm wrong in my equals method overriding.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Why did you change the validation method ? You are overriding equals() to be able to use the contains() method.
Varun Chopra
Ranch Hand

Joined: Jul 10, 2008
Posts: 211
Bharath Raja wrote:
Now., the problem is whenever I'm executing my code, my overrided equals() method not getting called. Instead of that default Object classes equals() method only called. Can any one find out where I'm wrong in my equals method overriding.



Have you added any object to the list before you call validation method? Can you paste the code where you create objects, add to list and call validate method?


-Varun -
(My Blog) - Online Certifications - Webner Solutions
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
Christophe Verré wrote:Why did you change the validation method ?

I thought that, overrided equals() method getting called only if I'm calling the equals() method.
Christophe Verré wrote: You are overriding equals() to be able to use the contains() method.

sorry .. I don't have idea about that.. can you please elaborate.. please don't get tensed.
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
Varun Chopra wrote:
Have you added any object to the list before you call validation method?


yes varun.. At first time the list is empty and the equals() method not getting control.. But at second it should have control right, because of the list has item what we have inserted...
Varun Chopra
Ranch Hand

Joined: Jul 10, 2008
Posts: 211
Bharath Raja wrote:
yes varun.. At first time the list is empty and the equals() method not getting control.. But at second it should have control right, because of the list has item what we have inserted...


Please post your code here, it is hard to find the problem without code.
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
Varun Chopra wrote:
Please post your code here, it is hard to find the problem without code.

Please find the code in my first post.. that's I have originally.
Varun Chopra
Ranch Hand

Joined: Jul 10, 2008
Posts: 211
I see validation(my,myList); method called only once in that piece of code, that also when myList is empty.
Can you paste whole code, the main method and also your MyClass again?

Here's similar code from my end that works:


Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
Varun Chopra wrote:I see validation(my,myList); method called only once in that piece of code, that also when myList is empty.
Can you paste whole code, the main method and also your MyClass again?


varun..actually this program is in swing, the validation() method getting called whenever I'm clicking the button. So that I'm sure I'm not wrong in my main class.. Because I have checked with eclipse debug mode also..

Please Note: code I have already submitted here is just for ranch purpose.. and sorry.. If I'm posting the original code then its my company's security breaches... Please try to realize that
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

The List#contains method will call the overrided equals() method. You don't have to call it.
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
Christophe Verré wrote:The List#contains method will call the overrided equals() method. You don't have to call it.


oh... o.k.. now I've changed the method as what I have earlier(removing Iterator and...)


but still my overrided equals() method not getting called... I'm not came up where I was wrong...
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

I used Varun's code, and replaced the validation method with yours. It works. So obviously there's something you're not doing right.

What's in your list ?
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
Problem solved... I have done silly mistake.. I've overrided the equals() method in wrong class (Instead of MyClass here I was put it in class which calling overrided equals() method, which have validation() method)... After seen the varun's code only I came to know I was wrong in there... So Based on varun's code, I've concluded that, if we are overrided the equals() method within the class AnyTest(varun's code) instead of write it in MyClass(varun's code), the overrided equals() method not getting called, the default equals() method only called... But chris/varun/ritchie, may I know why??? I think, still I'm a small baby in Java.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39882
    
  28
Never, never, write == true or == false.

Are you suggesting you can have the equals() method called in one class when you have overridden it in another class? Please show us the code.
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
Campbell Ritchie wrote: Please show us the code.

once again I apologize for unable to posting the original code. Instead of that I have changed the varun's code which will have a problem.


In above code, I have put the overrided equals() method into AnyTest class instead of MyClass.. Please try to execute this code..
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

At least I hope that you understood how the contains method works.
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
Christophe Verré wrote:At least I hope that you understood how the contains method works.

yes chris.. I got the contains() method, its implicitly calls the equals() method,,, but that is also not working, if we are changing our validation() method code( in my previous post)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39882
    
  28
It won't work since you haven't overridden the equals() method. You have an incorrectly overridden method in "test" (not "AnyTest"), as well.
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
Campbell Ritchie wrote:It won't work since you haven't overridden the equals() method. You have an incorrectly overridden method in "test"

I'm not clear here... how can you say I'm not overriding equals() there.. and..
Campbell Ritchie wrote:
not "AnyTest", as well

sorry for that
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Bharath Raja wrote:
Campbell Ritchie wrote:It won't work since you haven't overridden the equals() method. You have an incorrectly overridden method in "test"

I'm not clear here... how can you say I'm not overriding equals() there..

You are overriding equals() in the test class. But that's not where it needs to be overridden. Look at your validation() method - what object is it calling the equals() method on? What class is that object?
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
Mike Simmons wrote:
You are overriding equals() in the test class. But that's not where it needs to be overridden. Look at your validation() method - what object is it calling the equals() method on? What class is that object?


yes.. I'm clear now.. thanks to all ranchers...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: List's contains(Object o) method doesn't work for object version.