Dev@Free

          zJun's Tech Weblog

          剔除List中的重復值

          方法一:循環元素刪除
          // ?刪除ArrayList中重復元素
          public ? static ? void ?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);
          ????? }

          ????}

          ??}

          ??System.out.println(list);
          }

          方法二:通過HashSet剔除
          // ?刪除ArrayList中重復元素
          public ? static ? void ?removeDuplicate(List?list)? {
          ????HashSet?h?
          = ? new
          ?HashSet(list);
          ????list.clear();
          ????list.addAll(h);
          ????System.out.println(list);
          }

          方法三 刪除ArrayList中重復元素,保持順序
          // 刪除ArrayList中重復元素,保持順序
          public ? static ? void ?removeDuplicateWithOrder(List?list)? {
          ??? ? Set?set?
          = ? new
          ?HashSet();
          ???? ?List?newList?
          = ? new
          ?ArrayList();
          ??
          for ?(Iterator?iter? = ?list.iterator();?iter.hasNext();)?
          {
          ???????? Object?element?
          =
          ?iter.next();
          ????????
          if
          ?(set.add(element))
          ??????????? newList.add(element);
          ?????}

          ???? list.clear();
          ???? list.addAll(newList);
          ??? ?System.out.println(
          " remove?duplicate " ? + ?list);
          ?}

          posted on 2007-01-18 13:48 zJun's帛羅閣 閱讀(4637) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          <2007年1月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          統計

          常用鏈接

          留言簿(15)

          隨筆分類

          隨筆檔案

          相冊

          收藏夾

          博客

          文檔

          站點

          論壇

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 石渠县| 嘉祥县| 兴义市| 龙州县| 宜都市| 临沭县| 商水县| 万年县| 凌源市| 墨玉县| 白水县| 曲松县| 保靖县| 剑川县| 东阿县| 黄山市| 河津市| 巴南区| 大余县| 塔河县| 古浪县| 卢氏县| 灵宝市| 永德县| 长春市| 望城县| 石河子市| 扶绥县| 辛集市| 临邑县| 保靖县| 五家渠市| 汾阳市| 黑河市| 黎城县| 饶阳县| 葵青区| 孟村| 绵阳市| 扶沟县| 武川县|