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)

          隨筆分類

          隨筆檔案

          相冊

          收藏夾

          博客

          文檔

          站點

          論壇

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 扎鲁特旗| 蓝田县| 平顺县| 法库县| 依兰县| 清丰县| 黄平县| 枞阳县| 崇仁县| 沂南县| 凯里市| 祥云县| 咸阳市| 石城县| 文安县| 缙云县| 荃湾区| 昆明市| 宜城市| 梅河口市| 弋阳县| 临安市| 台安县| 长宁县| 灌云县| 巨鹿县| 大新县| 麻城市| 房产| 吉安县| 襄垣县| 同江市| 邯郸市| 临泉县| 辽宁省| 丰城市| 新和县| 林西县| 辽阳市| 北安市| 荔波县|