• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clarification about equals method of ArrayList

 
Raghavendra Desoju
Ranch Hand
Posts: 95
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
Marshal
Pie
Posts: 22122
88
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: 10662
144
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic