• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collections in java

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.




 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic