This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes Regarding the hashcode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Regarding the hashcode" Watch "Regarding the hashcode" New topic
Author

Regarding the hashcode

sarma kiran
Ranch Hand

Joined: Jul 17, 2008
Posts: 30
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Jul 17, 2008
Posts: 30
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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39102
    
  23
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

Joined: Jul 17, 2008
Posts: 30
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

Joined: Mar 06, 2001
Posts: 13459

Yes, sort of.

The equals and hashCode method contracts are interdependent. For more information see the API for Object.hashCode()
 
GeeCON Prague 2014
 
subject: Regarding the hashcode