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帛羅閣 閱讀(4640) 評論(0)  編輯  收藏


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


          網站導航:
           

          導航

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

          統計

          常用鏈接

          留言簿(15)

          隨筆分類

          隨筆檔案

          相冊

          收藏夾

          博客

          文檔

          站點

          論壇

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 繁峙县| 长岭县| 鄱阳县| 九台市| 海丰县| 平乡县| 麻城市| 通江县| 都昌县| 镇巴县| 泽库县| 义乌市| 台南市| 江城| 梅州市| 叙永县| 邯郸县| 长武县| 腾冲县| 德格县| 聂拉木县| 五指山市| 莎车县| 丰县| 当涂县| 大化| 大连市| 米泉市| 南丹县| 五莲县| 宾阳县| 洛阳市| 如东县| 永胜县| 丰台区| 剑阁县| 清水县| 保定市| 丰镇市| 寿阳县| 南阳市|