Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals hashcode

 
Alfred Nobel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a Class

I am using this object in Arraylist and sorting the list based on this. Then i am adding the list of this objects(List<ClassA> to HashSet, Then i am populating another List<ClassA> if it is not there in hashset.


The question here is should i have to implement equals and hashcode methods in ClassA. If so caan anyone give an efficient equal and hashcode impl code.

Thanks in advance
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manoj,
The first step is to define what you want object equality to mean. (just id or all fields, what level of granularity for the date)

Note that nobody here will write your code for you. We will help you so do feel free to post your first attempt here for comments/direction.
 
Pankaj Mittal
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code means you want to sort A class's objects array....

No you don't need to implement either of these methods because you are comparing on basis of date and date already has these methods.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manoj lal please UseAMeaningfulSubjectLine
"equals hashcode" makes little or no sense at all.

Also please take some time to read the ask good questions link below.


Hope this helps
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic