Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding the hashcode

 
sarma kiran
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Team {

private static final int HASH_PRIME = 1000003;
private String name;
private int wins;
private int losses;


public Team(String name) {
this.name = name;
}

public Team(String name, int wins, int losses) {
this.name = name;
this.wins = wins;
this.losses = losses;
}


/**
* this overrides equals() in java.lang.Object
*/
public boolean equals(Object obj) {
/**
* return true if they are the same object
*/
if (this == obj)
return true;

/**
* the following two tests only need to be performed
* if this class is directly derived from java.lang.Object
*/
if (obj == null || obj.getClass() != getClass())
return false;

// we know obj is of type Team
Team other = (Team)obj;

// now test all pertinent fields ...
if (wins != other.wins || losses!= other.losses) {
return false;
}

if (!name.equals(other.name)) {
return false;
}

// otherwise they are equal
return true;
}


/**
* This overrides hashCode() in java.lang.Object
*/
public int hashCode() {
int result = 0;

result = HASH_PRIME * result + wins;
result = HASH_PRIME * result + losses;
result = HASH_PRIME * result + name.hashCode();

return result;
}

}


Here i have one small program. I have one doubt y the result in the hashcode is computed thrice.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not computed three times, it is built up from the available values in three steps.

The algorithm is beyond me and most people, but we are assured By Joshua Bloch and others that building hash values like that makes them work well.
 
sarma kiran
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is we need to follow the same steps or we can you use some other way.

And is we need to compulsary use all the variables which are used in the equals method to compute the hashcode.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thais is because to get the unique hash code , When an objects have same number of wins, same number of losses and name , then they are expected to returns the same hash code .

This is what API says for hashCode method .

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49447
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason for using a prime number as a factor is to obtain the greatest possible variation in the low-order bits of the hashCode.
 
sarma kiran
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm.. thanks for quick response.

is we need to compulsary use all the variables which are used in the equals method to compute the hashcode.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, sort of.

The equals and hashCode method contracts are interdependent. For more information see the API for Object.hashCode()
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic