• Post Reply Bookmark Topic Watch Topic
  • New Topic

Collections in java  RSS feed

 
Dikshant Agarwal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new in Collection, as per my knowledge I have already override the hashCode() and Equals() method in Data class, but when I trying to search the element it is giving "not found". Why? Please look at the code below aand help me to find the bug.




 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You overloaded your method instead of overriding it.
Always use @Override annotation when overriding methods (using it produces a compiler error when you make a mistake in overriding).

Your method signature should be
Also, your method does not stick to the contract of Object#equals.
It will throw NullPointerException if you pass null to it.
According to the contract it should return false.

By the way, please UseCodeTags (← click this) when posting. I added them for you this time. Doesn't it look better?
 
Dikshant Agarwal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when I am using public boolean equals(Object obj),it is giving error.and ya I will keep in mind about [code tags ]in future.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dikshant Agarwal wrote:But when I am using public boolean equals(Object obj),it is giving error

It would help if you told us what the error is.
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dikshant Agarwal wrote:But when I am using public boolean equals(Object obj),it is giving error.

You need to use the signature I posted.
Writing an equals method is not trivial.
Check this out http://www.javaranch.com/journal/2002/10/equalhash.html
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dikshant Agarwal wrote:But when I am using public boolean equals(Object obj),it is giving error.and ya I will keep in mind about [code tags ]in future.

Dikshant,

I understand that you're new to our forums, but when you ask questions, you must read the responses thoroughly. The point about code tags is simply about making your threads readable on our site; and has nothing to do with the problem you asked about.

Pawel and Joanne have told you precisely what you need to do; so if you're still having problems then you need to TellTheDetails (←click).

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!