Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

B&S : how to implement equal and hash

 
Vaishali Paramane
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Xabier Martija
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Xabier

Its definetly helpful for me

Thanks

Vaishali
 
Mark Ebeling
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vaishali Paramane
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Vaishali Paramane
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Gabriel & Xabier

Your solution definetly helps me to implement hashCode
 
Xabier Martija
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gabriel Vargas
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic