夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks
          問題:
          下面的代碼試圖利用
          HashMap的Iterator對象遍歷該HashMap并刪除滿足條件的元素(比如超時的元素),但會拋出java.util.ConcurrentModificationException異常
              public static void main(String[] args)
                  
                  HashMap<String, String> hs=new HashMap();    
                  hs.put("p1", "1");
                  hs.put("p2", "1");
                  hs.put("p3", "1");
                  hs.put("p4", "1");
                  hs.put("p5", "1");
                  hs.put("p6", "1");       
                  Iterator it=hs.keySet().iterator();      
                  while(it.hasNext())
                  {
                      String str=(String)it.next();               
                      System.out.println(hs);   

                       //邏輯處理.........         
                       .............
                      hs.remove(str);      
                   
          }
              原因應該
          hs.remove(str)后,it內(nèi)容沒變,并且it里的指針列表又重新排序,所以只要確保刪除任一元素后,it保持同步更新即可:
              解決方案一:
          刪除任一元素后,it保持同步更新
              ............
                
          Iterator it=hs.keySet().iterator();   
                  while(it.hasNext())
                  {
                      it=hs.keySet().iterator();  
                      String str=(String)it.next();               
                      System.out.println(hs);   

                       //邏輯處理.........         
                       .............
                      hs.remove(str);      
                   

              ...........
              這樣的時間復雜度明顯太大(兩層循環(huán)嵌套)
              解決方案二:
          由于刪除元素時,hs的iterator對象也重新排序,所以只要用hs的一個副本hsBack
          Uackp的iterator去遍歷hs即可,這樣在刪除hs元素時iterator就不會重排了(因為刪除的是hs的元素,而不是該iterator所屬的
          hsBackUackp
          ...................
                  hsBackUp=(HashMap<String, String>)hs.clone();
                  Iterator it=hsBackUp.keySet().iterator();
                  System.out.println(hsBackUp);
                  while(it.hasNext())
                  {
                      String str=(String)it.next();               
                      System.out.println(hs);               
                      hs.remove(str);       
                   
          .....................
              這樣雖然時間復雜度小了(只有一層循環(huán)),可是空間復雜度大了(多了一個hashmap的拷貝);
              查閱api文檔和相關(guān)資料后,原來iterator對象有一remove方法:
          void remove()       
          Removes from the underlying collection the last element returned by the
          iterator (optional operation). This method can be called only once per
          call to
          next. The behavior of an iterator is unspecified if
          the underlying collection is modified while the iteration is in
          progress in any way other than by calling this method.

          于是有下面的改進:
          解決方案三:
          ..............................
          Iterator it=hs.keySet().iterator();
          while(it.hasNext())
          {
          String str=(String)it.next();
          System.out.println(hs);
          it.remove();
          }
          ..............................
          posted on 2011-11-05 01:42 HUIKK 閱讀(16133) 評論(1)  編輯  收藏 所屬分類: Java

          評論

          # re: 利用java迭代器Itetator遍歷并刪除HashMap中的元素問題 2014-11-25 09:47 柳qing
          本來簡單的東西被樓主講復雜了。。。  回復  更多評論
            

          主站蜘蛛池模板: 静安区| 疏附县| 新泰市| 广南县| 莱阳市| 云龙县| 易门县| 葫芦岛市| 青铜峡市| 大埔区| 图们市| 陆良县| 顺义区| 阜康市| 永仁县| 黔江区| 清镇市| 鹤山市| 南平市| 苏尼特左旗| 临安市| 乌兰县| 兴宁市| 固镇县| 会宁县| 屯昌县| 闽侯县| 广南县| 南通市| 恩平市| 合作市| 永康市| 玉林市| 隆子县| 瑞昌市| 英山县| 临洮县| 宜兰县| 巴里| 余江县| 许昌市|