File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equals hashcode" Watch "equals hashcode" New topic

equals hashcode

Alfred Nobel

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

Joined: May 26, 2003
Posts: 33102

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.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, 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

Joined: Jun 06, 2007
Posts: 2851

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

|Asking Good Questions|
I agree. Here's the link:
subject: equals hashcode
It's not a secret anymore!