• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on the CONTAINS method of List  RSS feed

 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to see if an element in my first List object exists is contained in my second List object by the following loop code:



What I'm finding is if the element is contained in the commonListTo object the boolean value still comes back as false. I guess I'm not understanding how the contains() is suppose to work. Any help, insight or direction would be greatly appreciated. Thanks.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this snippet:



You are not comparing the element of the 'from' list to elements in the 'To' list. By calling the getItemNum() method, you are checking if a property (i.e. the item number) of an element in the 'From' list is in the 'To' list. Was that your intent?

Also, what class is CommonList? (i.e. What API or library is it from; or is it a class you wrote?) And am I safe to assume that the getCommonListItems() returns a List?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On top of what Mark already said you also need to make sure that the object you are checking overrides the equals method. Especially if your comparing an object in memory to another object in memory of the same type. If you are comparing the pointer then its not that important.
 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,

That exactly was my intent. The CommonList is a class that I wrote and it does return a List<CommonListItem> commonListItems.

Thanks for the reply. A colleague stated what I had missed and that was that contains utilized the override on equals method that I had setup in my class.

I appreciate the help. Thanks again.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!