当前位置:网站首页>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 .
边栏推荐
- On December 8th, 2020, the memory of marketing MRC application suddenly increased, resulting in system oom
- 寄存器地址名映射
- Port occupation troubleshooting
- stm32和电机开发(从单机版到网络化)
- Expérience de port série - simple réception et réception de données
- C language pointer (special article)
- Storage of data in memory
- Interview question: general layout and wiring principles of high-speed PCB
- 外部中断实现按键实验
- [SVN] what is SVN? How do you use it?
猜你喜欢
C语言指针(特别篇)
JVM 内存结构 详细学习笔记(一)
Cesium load vector data
【云原生】DevOps(一):DevOps介绍及Code工具使用
嵌套(多级)childrn路由,query参数,命名路由,replace属性,路由的props配置,路由的params参数
Detailed learning notes of JVM memory structure (I)
Locust performance test 3 (high concurrency, parameter correlation, assembly point)
STM32的时钟系统
STM32串口寄存器库函数配置方法
【Istio Network CRD VirtualService、Envoyfilter】
随机推荐
STM32 clock system
Simulation volume leetcode [general] 1706 Where does the ball meet
Leetcode刷题记录(数组)组合总和、组合总和 II
Jenkins modifies the system time
Simulation volume leetcode [general] 1567 Length of the longest subarray whose product is a positive number
Several stages of PMP preparation study
C language pointer (Part 2)
Pycharm create a new file and add author information
External interrupt to realize key experiment
MySql数据库-索引-学习笔记
Original collection of hardware bear (updated on May 2022)
(3/8)枚举的不当用法 之 方法参数(二)
【SVN】SVN是什么?怎么使用?
Systick tick timer
串口实验——简单数据收发
JVM 内存结构 详细学习笔记(一)
数据在内存中的存储
[istio introduction, architecture, components]
Interface test API case, data and interface separation
Full link voltage test of the e-commerce campaign Guide