posts - 431,  comments - 344,  trackbacks - 0
          以前在java里面要想去除list中的重復數據可以使用兩種方式實現:
          1. 循環list中的所有元素然后刪除重復
              public   static   List  removeDuplicate(List list)  {
                for  ( int  i  =   0 ; i  <  list.size()  -   1 ; i ++ ) 
          {
                    for  ( int  j  =  list.size()  -   1 ; j  >  i; j -- ) 
          {
                         if  (list.get(j).equals(list.get(i))) 
          {
                            list.remove(j);
                          }

                      }

                    } 

                  return list;
              }

          2. 通過HashSet踢除重復元素
              public   static   List  removeDuplicate(List list)  {
                  HashSet h  =   new
           HashSet(list);
                  list.clear();
                  list.addAll(h);
                  return list;
               }

          在groovy中當然也可以使用上面的兩種方法, 但groovy自己提供了unique方法來去除重復數據
              def list = [1, 2, 3, 2, 4, 1, 5]
              list.unique()  // [1, 2, 3, 4, 5]
          posted on 2008-08-02 13:55 周銳 閱讀(3624) 評論(0)  編輯  收藏 所屬分類: Groovy&Grails
          主站蜘蛛池模板: 哈巴河县| 广州市| 射阳县| 英德市| 白山市| 微博| 闻喜县| 白水县| 红桥区| 蕲春县| 六枝特区| 郯城县| 沾益县| 德格县| 竹北市| 金塔县| 朝阳县| 祁门县| 晋城| 外汇| 涟源市| 宁都县| 敖汉旗| 民乐县| 苍梧县| 荣昌县| 永丰县| 沽源县| 六枝特区| 溧阳市| 石屏县| 扎鲁特旗| 眉山市| 伊春市| 西宁市| 托克托县| 弋阳县| 漳浦县| 桐乡市| 兴国县| 获嘉县|