About override hashCode () method

Category: Java EE
 
nuoweiy0412
2009-03-28 02:56:12

Sponsored Links
/ / Look at the red part is what does that mean? Please advise Thank you ~

package org.leegang.hrsystem.model;

import java.io.Serializable;
import java.util.Date;

public class Attend implements Serializable
{
private static final long serialVersionUID = ; 48L;

/ / represents the identifier property
private Integer id;
/ / Attendance Date
private String dutyDay;
/ / punch time
private Date punchTime;
/ / behalf of whether this duty punch punch
private boolean isCome;
/ / The type of attendance
private AttendType type;
/ / The attendance associated staff
private Employee employee;

/ / no-argument constructor
public Attend ( )
{
}
/ / constructor initializes all attributes
public Attend (Integer id, String dutyDay,
Date punchTime ;, boolean isCome,
AttendType type, Employee employee)
{
this.id = id;
this.dutyDay = dutyDay;
this.punchTime = punchTime;
this.isCome = isCome;
this.type = type;
this. employee = employee;
}

/ / id attribute setter and getter methods
public void setId (Integer id)
{
this.id = id;
}
public Integer getId ()
{
return this.id;
}

/ / dutyDay property setter and getter methods
public void setDutyDay (String dutyDay)
{
this.dutyDay = dutyDay;
}
public String getDutyDay ()
{
return this.dutyDay;
}

/ / punchTime property setter and getter methods
public void setPunchTime (Date punchTime)
{
this.punchTime = punchTime;
}
public Date getPunchTime ()
{
return this.punchTime;
}

/ / isCome property setter and getter methods
public void setIsCome (boolean isCome)
{
this.isCome = isCome;
}
public boolean getIsCome ()
{
return this.isCome;
}

/ / type attribute setter and getter methods
public void setType (AttendType type)
{
this.type = type ;
}
public AttendType getType ()
{
return this.type;
}

/ / employee attributes setter and getter methods
public void setEmployee (Employee employee)
{
this.employee = employee;
}
public Employee getEmployee ()
{
return this.employee;
}

/ / According to employee, isCome, dutyDay to rewrite
public boolean equals method ; equals (Object obj)
{
if (this == obj)
{
return true;
}
if (obj! = null &&
obj.getClass () == Attend.class)
{
Attend attend = (Attend) obj;
return getEmployee (). equals (attend.getEmployee ())
&& getDutyDay (). equals (attend.getDutyDay ( ))
&& getIsCome () == attend.getIsCome ();
}
return false;
}

public int hashCode ()
{
if (getIsCome () )
{
return dutyDay.hashCode () +
29 * employee.hashCode () + 1;
}
return dutyDay.hashCode () ; +
29 * employee.hashCode ();
}

}

Sponsored Links

fanshilong
2009-03-28 03:06:08

seems that you do not understand why To override the hashcode method, I suggest you go look
liu61355
2009-03-28 03:26:05
dutyDay.hashCode () +
29 * employee.hashCode ()

hashcode is the only object
whether employees sign
at what time the database as a composite primary key
so
dutyDay.hashCode () +
29 * employee.hashCode ()
and
+1 is to sign
fouriertree
2009-03-28 03:38:36
Halo nobody take a look at it?
myj2ee
2009-03-28 03:47:27
people do? Teach me yo ~
hliang120
2009-03-28 03:59:07
I checked the relevant information to understand why the use hashCode

But why here this method to be multiplied by 29 and then add one too? Other numbers okay? This very understanding
awivawiv
2009-03-28 04:10:49
I do not know
knowing only covered hashCode () method
xiaozhu541198284
2009-03-28 04:23:55
Halo there no one to help me to die? Uh `` `
fly_pig_sun
2009-03-28 04:29:08
hashCode () method indicates the format of the object in memory. Why then left with 29 +1 reason is not exactly clear.
abc332674047
2009-03-28 04:31:08
Digital is casual, just to produce different hashcode just
xDebussv
2009-03-28 04:34:21

brother ignorance ~ Why getIsCome () returns the result is true is dutyDay.hashCode () +
29 * employee.hashCode () + 1 do?

not meet the conditions do not need to add one?
jiangxi514
2009-03-28 04:38:26
very simple, because it uses the equals method dutyDay, emplyee and isCome, these three attributes are needed to produce just what different different hashcode, so he based on a combination of these three attributes have different The hashcode only, as dutyDay.hashCode () + m * employee.hashCode () + n, where m and n are you just selected, it does not matter, as long as no more than int range on the line
yuankai999
2009-03-28 04:43:43


ah That is just what combination will do right now is not necessarily dutyDay.hashCode () + m * employee.hashCode () + n this combination understand ~

But why are these three attributes? Oh and then pick what attributes when noting what?

Thank you very much ~
dianjujinghun
2009-03-28 04:46:16


pick the attributes in what should be noted that it? Thank you
xory2005
2009-03-28 05:01:31
And if the "29" position number specified must be a prime yo? Why?
mohan2008
2009-03-28 05:14:22
attribute any of these three changes - this class hashCode () points to the memory address on the change is not it?

not know if I understand right ~ uh `` `

say thank you for all of us for the first
liguohua131
2009-03-28 05:34:17
to Faster retrieval of different class object memory address ~

should be almost understood it Oh

Thank li_d_s slightly ~ Q Why there? Add me okay? 87547255
anling202008
2009-03-28 05:40:27
If a class does not mean that member property can not override hashcode () method? If you can be how to write?
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.