aspose file tools*
The moose likes Beginning Java and the fly likes Finding equal object in list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Finding equal object in list" Watch "Finding equal object in list" New topic
Author

Finding equal object in list

Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Is there some command that I can use to find out if there is an an element in an ArrayList that is equal to a given object (rather than *is* that object)?

I can't see anything in the docs... not to hard to iterate through every element and test if list.get(i).equals(x), but wondering if there's anything built in I can use.

Thanks
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

Did you not notice the contains method? Look at the definition of that.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Yep, but doesn't contains just return true if the object *is* the one specified, not just equal to it?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3689
    
  16
Luigi Plinge wrote:Yep, but doesn't contains just return true if the object *is* the one specified, not just equal to it?

Read the full description of the method


Joanne
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

OK case closed, thanks

Returns true if this list contains the specified element. 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)).
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

Luigi Plinge wrote:Yep, but doesn't contains just return true if the object *is* the one specified, not just equal to it?
If you read the API, it says "Returns true if this list contains the specified element. 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)). "
So, it depends on the specific implementation of the equals() method for the objects in question.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
Don't get me started about those stupid light bulbs.
 
subject: Finding equal object in list