• 对JAVA集合进行遍历删除时务必要用迭代器


    java集合遍历删除的方法:

    1、当然这种情况也是容易解决,实现方式就是讲遍历与移除操作分离,即在遍历的过程中,将需要移除的数据存放在另外一个集合当中,遍历结束之后,统一移除。

    2、使用Iterator遍历删除。

    使用Iterator遍历删除的原因:

    Iterator 是工作在一个独立的线程中,并且拥有一个 mutex 锁。 Iterator 被创建之后会建立一个指向原来对象的单链索引表,当原来的对象数量发生变化时,这个索引表的内容不会同步改变,所以当索引指针往后移动的时候就找不到要迭代的对象,所以按照 fail-fast 原则 Iterator 会马上抛出 java.util.ConcurrentModificationException 异常。
    所以 Iterator 在工作的时候是不允许被迭代的对象被改变的。但你可以使用 Iterator 本身的方法 remove() 来删除对象, Iterator.remove() 方法会在删除当前迭代对象的同时维护索引的一致性。

    今天同事写了几行类似这样的代码:

    public static void main(String args[]) {
        List<String> famous = new ArrayList<String>();
        famous.add("liudehua");
        famous.add("madehua");
        famous.add("liushishi");
        famous.add("tangwei");
        for (String s : famous) {
            if (s.equals("madehua")) {
                famous.remove(s);
            }
        }
    }

    运行出异常:

    Exception in thread "main" java.util.ConcurrentModificationException

    at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)

    at java.util.AbstractList$Itr.next(AbstractList.java:343)

    at com.bes.Test.main(Test.java:15)

    Java新手最容易犯的错误,对JAVA集合进行遍历删除时务必要用迭代器。切记。

    其实对于如上for循环,运行过程中还是转换成了如下代码:

    for(Iterator<String> it = famous.iterator();it.hasNext();){
             String s = it.next();
             if(s.equals("madehua")){
                 famous.remove(s);
             }
    }

    仍然采用的是迭代器,但删除操作却用了错误的方法。如将famous.remove(s)改成it.remove()

    则运行正常,结果也无误。

    当然如果改成:

    for (int i = 0; i < famous.size(); i++) {
                String s = famous.get(i);
                if (s.equals("madehua")) {
                    famous.remove(s);
                }
    }

    这种方法,也是可以完成功能,但一般也不这么写。

    为什么用了迭代码器就不能采用famous.remove(s)操作? 这种因为ArrayList与Iterator混合使用时会导致各自的状态出现不一样,最终出现异常。

    我们看一下ArrayList中的Iterator实现:

    private class Itr implements Iterator<E> {
       /**
        * Index of element to be returned by subsequent call to next.
        */
       int cursor = 0;
       /**
        * Index of element returned by most recent call to next or
        * previous.  Reset to -1 if this element is deleted by a call
        * to remove.
        */
       int lastRet = -1;
       /**
        * The modCount value that the iterator believes that the backing
        * List should have.  If this expectation is violated, the iterator
        * has detected concurrent modification.
        */
       int expectedModCount = modCount;
       public boolean hasNext() {
               return cursor != size();
       }
       public E next() {
               checkForComodification();
           try {
           E next = get(cursor);
           lastRet = cursor++;
           return next;
           } catch (IndexOutOfBoundsException e) {
           checkForComodification();
           throw new NoSuchElementException();
           }
       }
       public void remove() {
           if (lastRet == -1)
           throw new IllegalStateException();
               checkForComodification();
           try {
           AbstractList.this.remove(lastRet);
           if (lastRet < cursor)
               cursor--;
           lastRet = -1;
           expectedModCount = modCount;
           } catch (IndexOutOfBoundsException e) {
           throw new ConcurrentModificationException();
           }
       }
       final void checkForComodification() {
           if (modCount != expectedModCount)
           throw new ConcurrentModificationException();
       }
    }

    基本上ArrayList采用size属性来维护自已的状态,而Iterator采用cursor来来维护自已的状态。

    当size出现变化时,cursor并不一定能够得到同步,除非这种变化是Iterator主动导致的。

    从上面的代码可以看到当Iterator.remove方法导致ArrayList列表发生变化时,他会更新cursor来同步这一变化。但其他方式导致的ArrayList变化,Iterator是无法感知的。ArrayList自然也不会主动通知Iterator们,那将是一个繁重的工作。Iterator到底还是做了努力:为了防止状态不一致可能引发的无法设想的后果,Iterator会经常做checkForComodification检查,以防有变。如果有变,则以异常抛出,所以就出现了上面的异常。

    在java常用的集合框架就是list ,set ,map 。

          list 可通过下标进行遍历,set,map不能通过下表进行遍历,因此对于set ,map的数据遍历时,常常采用迭代器,不过在使用迭代器移除数据时存在陷阱。

          执行如下代码:

            Set set = new HashSet();
            set.add(1);
            set.add(2);
            set.add(3);
            Iterator i = set.iterator();
            while(i.hashNext()){
                Object o = i.next();
                set.remove(o);    
            }

    执行结果会出现以下异常:

    Exception in thread "main" java.util.ConcurrentModificationException
        at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
        at java.util.AbstractList$Itr.next(Unknown Source)

    这个问题在集合框架使用迭代器遍历数据时存在,在java5新曾的foreach遍历方式下也存在。在通过下表遍历list的过程中不存在这个问题。

    如:

     List list = new ArrayList();
     list .add(1);
     list.add(2);
     list.add(3);
     for(int i = 0 ; i < list.size();i++){
        list.remove(list.get(i));|| list.remove(i);
     }
    //或者使用iterator的remove方法

    代码能够正常执行。

    利用java迭代器Itetator遍历并删除HashMap中的元素问题

    问题:
    下面的代码试图利用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)

           //...................
            Map 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(); 
    } 
    //..............................
  • 相关阅读:
    opencv-python下简单KNN分类识别
    眼下智能手机市场的一点感想
    利用base64库暴力破解base加密
    python爬虫 模拟登陆校园网-初级
    四月的街道
    bzoj 1212 [HNOI2004] L语言(不用AC自动机)
    bzoj 1567 [JSOI2008]Blue Mary的战役地图题解
    bzoj1789 Necklace Y型项链
    bzoj 1957 土地购买
    网络流24题做题日记
  • 原文地址:https://www.cnblogs.com/duanxz/p/4750345.html
Copyright © 2020-2023  润新知