当前位置:网站首页>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
Iterable Interface has two important methods :
- 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 .
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 .
边栏推荐
猜你喜欢
随机推荐
Some pit avoidance guidelines for using Huawei ECS
Connecting mobile phone with ADB
Leetcode question brushing record (array) combination sum, combination sum II
JVM garbage collection detailed learning notes (II)
Cesium load vector data
STM32 clock system
Detailed learning notes of JVM memory structure (I)
Personal deduction topic classification record
C语言指针(特别篇)
C language pointer (exercises)
UnityShader入门精要个人总结--基础篇(一)
华为HCIP-DATACOM-Core_03day
Original collection of hardware bear (updated on June 2022)
Postman data driven
Analysis of Hessian serialization principle
Why is access to the external network prohibited for internal services of the company?
C language pointer (Part 1)
The use of recycling ideas
Postman interface test (II. Set global variables \ sets)
Three updates to build applications for different types of devices | 2022 i/o key review