Collection --- list

   Iterable are all super-class collection, collection inherited iterable, and the set list and also inherited the collection, and define an iterator Iterable in the () method returns an iterator Iterator

Iterator is a superclass and interface that provides a common user interface for a variety of containers, inside hasNext (), next (), remove () method of three

    There are sub-interfaces ListIterator and XMLEventReader

  In the ArrayList  

     List<String> list=new ArrayList<>();
          list.add("abc");
          list.add("edf");
          list.add("ghi");
          for(Iterator<String> it=list.iterator();it.hasNext();)
          {
              System.out.println(it.next());
          }

    ArrayList defined within an inner class Itr, which implements the Iterator interface.

      In Itr, there are three variables, Cursor: index indicates a position of the lower element, lastRet: expected number of modified: the index indicates a position of an element, expectModCount

        

  Traversal issue you can not delete elements in the collection:

    If you increase list.remove ( "abc") at the top of the for loop, there will be ConcurrentModificationException exception.

    Because before you iteration, the iterator has been through list.itertor () to create out, if in the process of iteration, has been on the list to change the size of its container operations, then Java will give an exception.

      Because at this time Iterator object has been unable to take the initiative to make changes in the synchronization list, Java will think you make such an operation is not thread-safe, will give a reminder of goodwill (ConcurrentModificationException throws an exception)

      ArrayList in the source code implementation        

        private class Itr implements Iterator<E> {
          int cursor; // index of next element to return
          int lastRet = -1; // index of last element returned; -1 if no such
          int expectedModCount = modCount;

          Boolean the hasNext public () {
            ! = return cursor size; // When the cursor is not equal size, still represents the index element

          }

          @SuppressWarnings("unchecked")
          public E next() {
            checkForComodification();
            int i = cursor;
            if (i >= size)
              throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
              throw new ConcurrentModificationException();
            cursor = i + 1;
            return (E) elementData[lastRet = i];
          }

          public void remove() {
            if (lastRet < 0)
              throw new IllegalStateException();
            checkForComodification();

            try {
              ArrayList.this.remove(lastRet);
              cursor = lastRet;
              lastRet = -1;
              expectedModCount = modCount;
              } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
              }
            }

          @Override
          @SuppressWarnings("unchecked")
          public void forEachRemaining(Consumer<? super E> consumer) {
             Objects.requireNonNull(consumer);
             final int size = ArrayList.this.size;
             int i = cursor;
             if (i >= size) {
              return;
             }
            final Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length) {
              throw new ConcurrentModificationException();
            }
            while (i != size && modCount == expectedModCount) {
            consumer.accept((E) elementData[i++]);
            }
            // update once at end of iteration to reduce heap write traffic
            cursor = i;
            lastRet = i - 1;
            checkForComodification();
          }

          final void checkForComodification() {
            if (modCount != expectedModCount)
              throw new ConcurrentModificationException();
          }
      }

  通过查看源码发现原来检查并抛出异常的是checkForComodification()方法。在ArrayList中modCount是当前集合的版本号,  

    每次修改(增、删)集合都会加1;expectedModCount是当前迭代器的版本号,在迭代器实例化时初始化为modCount。我们看到在                                                 checkForComodification()方法中就是在验证modCount的值和expectedModCount的值是否相等,

    So when you call ArrayList.add () ArrayList.remove (), the only updates the status of modCount, while the iterator expectedModCount not synchronized,

       So will result in a call Iterator.next again () method throws an exception.

  But why use Iterator.remove () there is no problem? By highlighting the source code line found in the synchronized value expectedModCount remove Iterator of () in, so when the next time you call next (), the check will not be thrown.

  

loop iterator for comparison:

  *  Each have their own advantages Efficiency:

     ArrayList faster random access, and used for loop get () method, i.e. method using the random access, so in ArrayList for fast cycle.

     LinkedList sequential access is relatively fast, the Iterator Next () method used is sequential access method, so faster in LinkedList used in Iterator.

     Mainly to be based on a different set of data structures judgment.

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/xp0813/p/11074104.html