Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes equals hashcode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equals hashcode" Watch "equals hashcode" New topic
Author

equals hashcode

Alfred Nobel
Greenhorn

Joined: Nov 09, 2007
Posts: 5
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


SCJP 1.4<br />SCWCD 1.4
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30789
    
157

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Pankaj Mittal
Ranch Hand

Joined: Jul 09, 2008
Posts: 32
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.


2008 SCJP(5.0) 88%
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

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


SCJP, SCWCD.
|Asking Good Questions|
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals hashcode