• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on the CONTAINS method of List

 
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic