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

contains() & equals()!

Christina Smith
Greenhorn

Joined: Apr 05, 2005
Posts: 12
hello

for some reason i cant get my add() to work...
within SomeClass ive got

and within the TheObject class


as far as i know, the equals() method should be overridden in TheObject class since thats what the list in SomeClass is holding (objects of type TheObject).
BUT, are there any other factors that i need to consider when using contains()? i just cant c y its not working when the equals() is!

Every1's help is greatly appreciated..
Christina
[ April 06, 2005: Message edited by: Christina Smith ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Some Collections, like TreeSet could use the compareTo method to determine whether to add an item, and if the compareTo method returns different results than the equals() method, you will find some weird behavior.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Christina Smith
Greenhorn

Joined: Apr 05, 2005
Posts: 12
thanks
does that mean for treeSet i need to extend Comparable interface?
im not using it by the way, but i'd like to know.
chris
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Christina Smith:
...does that mean for treeSet i need to extend Comparable interface? ...

The method of comparison depends on the TreeSet constructor. If you pass a Comparator as a constructor argument, then it will use that Comparator. Otherwise, the elements must implement Comparable.

API: http://java.sun.com/j2se/1.5.0/docs/api/java/util/TreeSet.html

If you're adding elements to a List only if they're not already in the List, then you might consider using a Set instead of a List. Not only does the Set reject duplicates, but its add method returns a boolean telling you whether the element was added.
[ April 06, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Christina Smith
Greenhorn

Joined: Apr 05, 2005
Posts: 12
yes i c..
the problem is that we're already given the list with 10 objects and we need to mess around with it (add, remove) and so contains() & equals() are expected to be used... but i dont know y mine isnt working! the equals as i commented works fine, but when i add an object it doesnt check if its already there (thats based on the ID)! is there anything i might have missed out!?

thanx guys
Christina
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Hmmm... Could you post the code for TheObject?

Also, what kind of List is this (ArrayList, LinkedList...)?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

From the Java API Javadocs for Treeset


Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.


Mark
Christina Smith
Greenhorn

Joined: Apr 05, 2005
Posts: 12
its an ArrayList
TheObject has got things similar to this,


thank u
Chris
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

This looks like it should work.

I added a main method that generates 10 instances of TheObject with id's of "ID-x" where x is a random number 1-10. This way, we're (almost) certain to get duplicates. After attempting to add each item, the size of the list is also printed. This appears to demonstrate that non-duplicates (per equals) are added and duplicates are not added.

I removed some of the code that didn't apply, but I didn't change any of the critical parts.

What are you seeing that makes you think it's not working?

[ April 06, 2005: Message edited by: marc weber ]
Christina Smith
Greenhorn

Joined: Apr 05, 2005
Posts: 12
thats what i cant pick up! y isnt mine working! its adding duplicates! when i add 2 things with the same id it accepts it!
by the way, u dont seem have considered the interface, does it not matter?
can u please check ur private messages

christina
[ April 07, 2005: Message edited by: Christina Smith ]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
another thing to keep in mind: always override hashCode() when overriding equals(Object)

Might not be relevant here (but then again it might) but it's a good practice to get into.


42
Christina Smith
Greenhorn

Joined: Apr 05, 2005
Posts: 12
whats the hashCode? and when do i need to override it?
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Josh Bloch: Effective Java
JavaWorld: Implementing equals and hashCode
Sun's Java Tutorial: The equals and hashCode Methods
IBM: Defining hashCode() and equals() effectively and correctly
JavaPractices: Implementing hashCode
JavaWorld: override hashcode/equals
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Christina Smith:
...can u please check ur private messages...

I don't know why you can't post your actual code. It's not fair to those of us trying to help if you post "simulated" code (especially if it's not accurate), and it's not fair to anyone following this thread to take it offline.

I'll just say this: A HashSet is not an ArrayList.

Jeroen is right -- you need to override the hashCode method. See Steve's links above (especially Sun's Tutorial).
 
wood burning stoves
 
subject: contains() & equals()!