• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding equal object in list

 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you not notice the contains method? Look at the definition of that.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, but doesn't contains just return true if the object *is* the one specified, not just equal to it?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12188
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic