• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

contains() & equals()!

 
Christina Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Christina Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Christina Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... Could you post the code for TheObject?

Also, what kind of List is this (ArrayList, LinkedList...)?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its an ArrayList
TheObject has got things similar to this,


thank u
Chris
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christina Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whats the hashCode? and when do i need to override it?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic