当前位置:网站首页>Difference between interface iterator and iteratable

Difference between interface iterator and iteratable

2022-07-07 09:20:00 Wow, it's a small dish

Recently, when looking at the source code of the collection, I found that these two interfaces are easily confused , So I studied .

Iteratble Interface

 Insert picture description here

Iterable Interface has two important methods :
 Insert picture description here

  • 1.Iterator iterator();
    Returns an iterator , Iterators have a series of functions , You can traverse the collection or delete elements .
  • 2.void forEach(…);
    This method is for the collection for Loops offer possibilities , If the implementation class does not implement this interface , You can't do it for loop .

Iterator Interface

Actually Iterator An interface is an interface to be implemented , An interface represents a specification , A common behavior .Iterator That's iterator , It's a tool .

 Insert picture description here
therefore , The implementation class overrides the interface iterator() Method , Returned the iterator suitable for your use , by JDK Our users provide this convenient tool .

Here are AbstractorList Class to itrator() Method implementation , Will return a new Itr,Itr Is an inner class , The specific code is as follows :

private class Itr implements Iterator<E> {
    
        /** * Index of element to be returned by subsequent call to next. */
         //  This pointer 
        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. */
         //  Previous pointer 
        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. */
         //  Number used to detect illegal deletion when iterating over the set 
         // modCount Understood as the number of operations 
        int expectedModCount = modCount;
		
		//  Whether the set has been iterated 
        public boolean hasNext() {
    
            return cursor != size();
        }
		
		//  The pointer moves down ,lastRet Point to the position of the current pointer 
        public E next() {
    
            checkForComodification();
            try {
    
                int i = cursor;
                E next = get(i);
                lastRet = i;
                cursor = i + 1;
                return next;
            } catch (IndexOutOfBoundsException e) {
    
            	//  This method is used to detect concurrent operations , That is, the same set cannot be operated by two threads at the same time , Will throw an error 
                checkForComodification();
                throw new NoSuchElementException();
            }
        }
		
		/** *  Remove the previous element indicated by the pointer  */
        public void remove() {
    
            if (lastRet < 0)
                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();
        }
    }

in summary , Iterators are used more , still next(); and hasNext(); Method , Never iterate while remove(); Otherwise, an error will be reported .

原网站

版权声明
本文为[Wow, it's a small dish]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130626221247.html