• Post Reply Bookmark Topic Watch Topic
  • New Topic

Clarification about equals method of ArrayList  RSS feed

 
Raghavendra Desoju
Ranch Hand
Posts: 113
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


this outputs:
true
true
true

I am wondering how two.add(new String("hawk")); still produces true when compared with one.

Looks like equals has a custom implementation which compares the actual values rather than reference equality.

Thanks,
Raghu
 
Henry Wong
author
Sheriff
Posts: 23291
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghavendra Desoju wrote:
Looks like equals has a custom implementation which compares the actual values rather than reference equality.


as documented in JavaDoc for the List interface
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghavendra Desoju wrote:Looks like equals has a custom implementation which compares the actual values rather than reference equality.

Yes indeed! Like Henry already mentioned it's mentioned in the equals method of the List interface. So any implementing class of the List interface must override the equals method and implement it according to the contract.

What do you think will be the output of this code snippet?

Hope it helps!
Kind regards,
Roel
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!