Posted on
Taipa onlineReading (724)
Comment (1)
Edit
FavoritesCategory:
Java-related
During Development yesterday, I encountered the following problem: I have an artistlist object of arraylist <artistentity>. Every time I add an object, I make a judgment on the added object artistentity, if artistlist does not contain artistentity, add the following:
If (! ArtistList. contains (artistEntity )){
ArtistList. add (artistEntity );
}
However, the running result is not as designed. The page display shows that artistlist contains two identical artistentity objects. Why?
I have read the jdk source code for this section:
/***//**
* Returns <tt> true </tt> if this list contains the specified element.
*
* @ Param elem element whose presence in this List is to be tested.
* @ Return <code> true </code> if the specified element is present;
* <Code> false </code> otherwise.
*/
Public boolean contains (Object elem ){
Return indexOf (elem)> = 0;
}
/***//**
* Searches for the first occurence of the given argument, testing
* For equality using the <tt> equals </tt> method.
*
* @ Param elem an object.
* @ Return the index of the first occurrence of the argument in this
* List; returns <tt>-1 </tt> if the object is not found.
* @ See Object # equals (Object)
*/
Public int indexOf (Object elem ){
If (elem = null ){
For (int I = 0; I <size; I ++)
If (elementData [I] = null)
Return I;
} Else {
For (int I = 0; I <size; I ++)
If (elem. equals (elementData [I])
Return I;
}
Return-1;
}
In this case, the equals of the Object is called directly for Object comparison. When this method is overwritten, it is usually necessary to override it.HashCodeMethod to maintainHashCodeMethod, which declares that the same object must have an equal hash code. I didn't rewrite it, so it caused the problem here.
Fix: directly use Eclipse to rewrite, right-click/source code/generate hashCode () and equals (), and select the fields to be included in the hashCode () and equals () methods:
/***//**
* @ Return
* @ See java. lang. Object # hashCode ()
*/
@ Override
Public int hashCode (){
Final int PRIME = 31;
Int result = 1;
Result = PRIME * result + (artstCd = null )? 0: artstCd. hashCode ());
Return result;
}
/***//**
* @ Param obj
* @ Return
* @ 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 (getclass ()! = Obj. getclass ())
Return false;
Final artistentity Other = (artistentity) OBJ;
If (artstcd = NULL ){
If (other. artstcd! = NULL)
Return false;
} Else if (! Artstcd. Equals (other. artstcd ))
Return false;
Return true;
}
That's all.
Alternatively, add an artistEntity object to compare the key value of artistEntity. Here I use another ArrayList <String> artistCodeList to store the key value of artistEntity. If artistCodeList does not contain the key of the ArtistEntity to be judged, the artistEntity is added to the artistList: // added only when the list does not contain
If (! ArtistCodeList. contains (artistEntity. getArtstCd ())){
ArtistCodeList. add (artistEntity. getArtstCd ());
ArtistList. add (artistEntity );
}
This article is transferred from
Http://www.blogjava.net/kxx129/archive/2007/01/24/95673.html