泰仔在线

java学习,心情日记,缤纷时刻
posts - 100, comments - 34, trackbacks - 0, articles - 0

由List.contains(Object, obj)想到的

Posted on 2007-01-24 10:03 泰仔在线 阅读(5629) 评论(1)  编辑  收藏 所属分类: Java 相关

   昨天在开发的时候,碰到这样一个问题:我有一个ArrayList<ArtistEntity>的对象artistList,每次在加入对象时,我都对加入的对象artistEntity进行了判断,如果artistList不包含artistEntity,才进行加入:

if  ( ! artistList.contains(artistEntity))  {
   artistList.add(artistEntity);
}

   但运行的结果并不是如开始设计的那样,页面显示表明artistList包含了两个相同的ArtistEntity对象,这是什么原因呢?
   我看了一下jdk关于这段的源代码:

     /**
     * 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 ;
    }
   这时会直接调用Object对象的equals进行对象比较,而当此方法被重写时,通常有必要重写 hashCode 方法,以维护 hashCode 方法的常规协定,该协定声明相等对象必须具有相等的哈希码。而我没有重写,因此就导致了这里的问题了。
   修正:直接利用Eclipse重写,右键/源代码/生成hashCode()和equals(),选择要包括在hashCode()和equals()方法中的字段:
    /**
     * 
@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;
    }
这样就好了。
或者,在加入artistEntity对象,比较artistEntity的key值。我这里是另外用一个ArrayList<String> artistCodeList存放artistEntity的key值,如果artistCodeList不包含待判断ArtistEntity的key时,才将artistEntity加入artistList:
// 当アーティスト情報列表中不包含时才加入
                    if (!artistCodeList.contains(artistEntity.getArtstCd())) {
                        artistCodeList.add(artistEntity.getArtstCd());
                        artistList.add(artistEntity);
                    }
 

Feedback

# re: 由List.contains(Object, obj)想到的  回复  更多评论   

2007-02-14 17:51 by sosn
好好学习天天向上

只有注册用户登录后才能发表评论。


网站导航: