views:

362

answers:

4

How should I implement hashCode() and equals() for the following class in Java?

class Emp 
{
  int empid ; // unique across all the departments 
  String name;
  String dept_name ;
  String code ; // unique for the department 
}
A: 

If code is unique (i.e. your business key), it's best to only use the code for equals and hashCode - it's good practice to seperate business key (code) from object id (id).

Here's a nice read: Hibernate Documentation: Equals and HashCode (valid not only for Hibernate itself)

sfussenegger
It seems to be unique for a department, so that suggests that departments can share the same "code". Anyway, a rather vague question if you ask me.
Bart Kiers
+2  A: 

in Eclipse right mouse click-> source -> generate hashCode() and equals() gives this:

/* (non-Javadoc)
 * @see java.lang.Object#hashCode()
 */
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((code == null) ? 0 : code.hashCode());
    return result;
}
/* (non-Javadoc)
 * @see java.lang.Object#equals(java.lang.Object)
 */
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (!(obj instanceof emp))
        return false;
    emp other = (emp) obj;
    if (code == null) {
        if (other.code != null)
            return false;
    } else if (!code.equals(other.code))
        return false;
    return true;
}

I've selected code as a unique field

jutky
Hmmm... I would usually compare all fields in the equals() method unless you really have a strong reason not to...
Neil Coffey
So if two `Emp` from different departments have the same code, they are equal? You need at least to add the `dept_name` in this implementation.
Pascal Thivent
I am accepting this answer as I got the idea what needs to be done . jutky has assumed code as the unique identifier.
mithunmo
A: 

what ever values you use in equals to determine if two objects are the same, are the the values that you need to use to create a hash code.

public boolean equals(Object o) {

    boolean result = false;

    if(o instanceof CategoryEnum) {

        CategoryEnum ce = (CategoryEnum) o;
        result = ce.toString().equals(name);

    }       
    return result;

}


public int hashCode()
{
  int hash = 6;
  hash += 32 * name.hashCode();
  return hash;
}   
jeff porter
(no -1 from me) That's not completely true. It's perfectly legal to only use a subset for hashCode (e.g. code and name for equals and only code for hashCode). It's even legal to use a constant hashCode (`public int hashCode() {return 42;}`) - it ruins the performance of hashed collections (HashMap, HashSet, ...) but they keep working correctly. So it's better than an invalid hashCode method. The only rule is: if two objects are equal (`a.equals(b)`) they must have the same hash code (`a.hashCode() == b.hashCode()`). If they aren't equal, hash codes may still be equal.
sfussenegger
A: 

Hello , equals()and hashcode(),They have a lot of different places. equals(),if we don't Override it from Object,it represent that whether two variables are pointing to the same object heap?

Blockquote

public Class Student(){

Blockquote

private int id;

private name; public Student(int id,String name){ this.name=name; this.id=id; }

Blockquote

} public void main(String[] args){ Student A=new Student(20,'Lily');

Student B=new Student(20,'Lily'); boolean flag=A.equals(B)//flag=flase; /* *Although they attribute the same, but they are two different objects, they point to different memory */

Blockquote Blockquote

Blockquote

@Override

Blockquote

public boolean equals(Object obj) {

Blockquote

if (obj == null) {
  return false;
}
if (this == obj) {
  return true;
}

if (this.getClass() != obj.getClass()) {
  return false;
}

Student s=(Student)obj; return new Integer(this.id).equals(new Integer(s.id))&&this.name.equals(s.name); }

Blockquote

/** *Sometimes even though we Override the equals, but we still can not determine whether the *two objects the same, *In the collection object, such as HashSet, this time we have to Override the hashoCode () */

Blockquote

public int hashCode(){

return id + name.hashCode() ;

}

Blockquote