泰仔在線

          java學習,心情日記,繽紛時刻
          posts - 100, comments - 34, trackbacks - 0, articles - 0

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

          Posted on 2007-01-24 10:03 泰仔在線 閱讀(5659) 評論(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
          好好學習天天向上
          主站蜘蛛池模板: 昭苏县| 东海县| 富川| 黄山市| 文昌市| 金塔县| 隆安县| 滁州市| 稻城县| 宾川县| 惠来县| 台东市| 上饶市| 凤山市| 夏河县| 靖江市| 万州区| 滦南县| 襄城县| 衡阳市| 临沂市| 阿拉善右旗| 黔西县| 静海县| 长白| 乐至县| 夏河县| 岳普湖县| 喀什市| 天祝| 邮箱| 永胜县| 泰和县| 合水县| 鄱阳县| 凌云县| 南澳县| 思茅市| 巴彦县| 黑河市| 农安县|