aspose file tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes B&S : how to implement equal and hash Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Reply Bookmark "B&S : how to implement equal and hash" Watch "B&S : how to implement equal and hash" New topic
Author

B&S : how to implement equal and hash

Vaishali Paramane
Ranch Hand

Joined: Mar 05, 2007
Posts: 106
Hi
I am just download B&S Assignment.
I am new in java development.
Any body help me to how to implement equal and hash if i have following field

name
location
specialties
rate
size
owner

I wrote following is this correct or any change?
public boolean equals(Object obj) {

if (!(obj instanceof ContractorInfo)) {
return false;
}

ContractorInfo info = (ContractorInfo) obj;

return (name == null) ? (info.getName() == null)
: name.equals(info.getName());
}

public int hashCode() {
return name.hashCode();
}

Thanks

Vaishali
[ July 18, 2007: Message edited by: Vaishali Paramane ]

SCJP, SCJD
Preparing for SCWCD
Xabier Martija
Ranch Hand

Joined: Jul 18, 2007
Posts: 40
It seems to be ok. But you must ask yourselfe if two objects are equal if their names are equal. You don't care for the other properties.
I also would add this lines a the top of the equal method:

public boolean equals(Object obj) {

if (obj==this)
return true;
Vaishali Paramane
Ranch Hand

Joined: Mar 05, 2007
Posts: 106
Thanks Xabier

Its definetly helpful for me

Thanks

Vaishali
Mark Ebeling
Ranch Hand

Joined: Jul 06, 2007
Posts: 38
Vaishali,
I also have the B&S project. I have decided to assume that the Name and Location together are the Keys to the contractor database. I figurre that there may be one contractor with many locations, but he will only have one location in a town. And I am documenting that in my choices. So for .equals I check to see if both the name && location are equivalent.

Thanks,
Mark


GREAT DAY TO BE ALIVE - Beats the alternative!<br />
SCJP 5.0
Vaishali Paramane
Ranch Hand

Joined: Mar 05, 2007
Posts: 106
Thanks Mark
But I am confusing how to write hashCode().
If only name is equal then we can return name as a hashcode but can you tell me how to implment hasCode? if we use the both name and location.
Xabier Martija
Ranch Hand

Joined: Jul 18, 2007
Posts: 40
The rules for hashcode are simple:

-If 2 objects are equal , then they MUST have the same hashCode.

-If 2 objects are NOT equal , they SHOULD have different hashCodes.

So different object could have the same hash but the performace would't be so fine.

public boolean equals(Object obj) {
if this==obj{
return true;
}

if (!(obj instanceof ContractorInfo)) {
return false;
}

ContractorInfo info = (ContractorInfo) obj;

return (name == null) ? (info.getName() == null)
: name.equals(info.getName()) && (location == null) ? (info.getLocation() == null)
: location.equals(info.getLocation());
}

public int hashCode() {
return name.hashCode();//this is good enough
//return name.hashCode()+ location.hashCode();//it's not better
}


the first hashCode version keeps beeing good enough. Every time two objects that own the same name will bee allocated in the same slot in a hashmap , for example, thats called a collision.

the second version , i don�t like it, becaus an object wiht name "a" and location "b" , would have the sema hash as an object with name "b" and location "a". That's not the sense of hashcode.
Gabriel Vargas
Ranch Hand

Joined: May 16, 2007
Posts: 145
Hi All,

My two cents. I prefer implement a hash code than is different for every object and avoid a "collision". I think it could be easy only concat name and location and probabily with a separator like a "-" or ":" or "|", or one of your choice.

So an object wiht name "a" and location "b", has a hashCode of one of these strings:

"ab" or "a-b" or "a:b" or "a|b".

Example:



I hope it helps you:


Gabriel Vargas
SCJP, SCJD, now studying for SCWCD and working to be a better person
Vaishali Paramane
Ranch Hand

Joined: Mar 05, 2007
Posts: 106
Thanks Gabriel & Xabier

Your solution definetly helps me to implement hashCode
Xabier Martija
Ranch Hand

Joined: Jul 18, 2007
Posts: 40
Yes , Gabriel's solution is the best!

Your solution well avoid colision better , but will well be still colision, since hashcode returns an int and there are more strings conbinations than ints.

The sense of hashcode is to have homogenous distribution among the posible hash values.
rinke hoekstra
Ranch Hand

Joined: Apr 06, 2007
Posts: 152
Hi all,

I don't have B&S, but UrlyBird. As I don't know what B&S is about, my choice is probably of no relevance at all for you guys, but probably there is someone out there who is interested in what to do for UrlyBird.

I figured that the Room object in urlybird should only use the record number for the equals and hashcode methods, because the record number is the only thing which is determining.

I know some people on this forum defined a "key" for urlybird records, but I think that is a big mistake. We are talking here about hotel rooms. A big hotel has many rooms in the offering. Why shouldn't it be possible that a hotel offers more than one room with exactly the same size and smoking yes/no on exactly the same date?? And, as the rooms are equal, it is also very likely they would have the same price.

I think defining a key on database fields puts unreasonably limiting restrictions on the functionality. As the primary keys of the database should be reflected in the Room object's equals and hashcode methods, same counts for these. So only recNo is determining equals and hashcode.


_ _ ________________________ _ _ <br /> <br />Just SCJP (but 93%)
Gabriel Vargas
Ranch Hand

Joined: May 16, 2007
Posts: 145
Hi all,

Interesting post of Rinke, i also have a key for each record defined by the logic position of the contractor begins with zero, i don't mix fields because i know it could have problems in the future becuase it doesn't maintain unique.
Vaishali Paramane
Ranch Hand

Joined: Mar 05, 2007
Posts: 106
rinke is right
but in my case sub contractor and city is enough for equal and hashCode.
Some guys also added the type of work in equal and hashCode.
no of employee, rate doesnt make sense in equal and hashCode otherwise it will may create problem further.

Thanks

Vaishali
Mark Ebeling
Ranch Hand

Joined: Jul 06, 2007
Posts: 38
Sorry to reply late, but I have been traveling. I also use only the name.hashcode() as my hashcode value for the Contractor object. I have a ContractorRecord object that uses recNo for the hashcode value. But collisions will happen, but that's okay. Hashing does not need to have unique values. Just even and wide distributions for performance, right? Anyway, I did pretty much what Xabier did. Good luck!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: B&S : how to implement equal and hash
 
Similar Threads
Properly overriding equals and hashCode
HashSet unique values
equals() method-Unable to understand
equals() and hashCode()
hashcode and equals