夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            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內容沒變,并且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);      
                   

              ...........
              這樣的時間復雜度明顯太大(兩層循環嵌套)
              解決方案二:
          由于刪除元素時,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);       
                   
          .....................
              這樣雖然時間復雜度小了(只有一層循環),可是空間復雜度大了(多了一個hashmap的拷貝);
              查閱api文檔和相關資料后,原來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 閱讀(16124) 評論(1)  編輯  收藏 所屬分類: Java

          評論

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

          主站蜘蛛池模板: 姜堰市| 渭南市| 元氏县| 睢宁县| 莱州市| 尉犁县| 兴隆县| 汪清县| 宜川县| 绍兴县| 乌拉特后旗| 云霄县| 沂南县| 怀化市| 龙游县| 曲松县| 阿克陶县| 东阳市| 遂宁市| 梁山县| 宁强县| 奉化市| 阳原县| 台北县| 海门市| 安阳县| 施甸县| 南投市| 景东| 弋阳县| 施秉县| 互助| 塘沽区| 利辛县| 田东县| 晋宁县| 中山市| 湘潭市| 万州区| 电白县| 浦县|