java  Iterator接口和LIstIterator接口分析

目录

1.Iterator接口

2.ListIterator

3.Iterator和ListIterator的区别

正文

在继续看ArrayList源码之前,先了解Iterator接口和ListIterator接口,下篇文章详细讲解ArrayList是如何实现它们的。

我们知道,接口只是一种规范,当继承接口并实现其中的方法时,要遵循接口对方法的说明。

1.Iterator接口

Iterator接口取代了Java集合框架中的Enumeratrion。Iterators不同于enumerations的地方主要有两点:

Iterators允许调用者在迭代过程中从集合里移除元素;

方法名得到了改善。

Iterator源码如下:

/**

* An iterator over a collection. {@code Iterator} takes the place of

* {@link Enumeration} in the Java Collections Framework. Iterators

* differ from enumerations in two ways:

* Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.

* Method names have been improved.

* This interface is a member of the Java Collections Framework.

* @param the type of elements returned by this iterator*/

public interface Iterator {

/**

* Returns {@code true} if the iteration has more elements.

* (In other words, returns {@code true} if {@link #next} would

* return an element rather than throwing an exception.)

* @return {@code true} if the iteration has more elements

*/

boolean hasNext();

/**

* Returns the next element in the iteration.

* @return the next element in the iteration

* @throws NoSuchElementException if the iteration has no more elements

*/

E next();

/**

* Removes from the underlying collection the last element returned

* by this iterator (optional operation). This method can be called

* only once per call to {@link #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.

*

* @implSpec

* The default implementation throws an instance of

* {@link UnsupportedOperationException} and performs no other action.

*

* @throws UnsupportedOperationException if the {@code remove}

* operation is not supported by this iterator

*

* @throws IllegalStateException if the {@code next} method has not

* yet been called, or the {@code remove} method has already

* been called after the last call to the {@code next}

* method

*/

default void remove() {

throw new UnsupportedOperationException("remove");

}

/**

* Performs the given action for each remaining element until all elements

* have been processed or the action throws an exception. Actions are

* performed in the order of iteration, if that order is specified.

* Exceptions thrown by the action are relayed to the caller.

*

* @implSpec

*

The default implementation behaves as if:

*

{@code

* while (hasNext())

* action.accept(next());

* }

*

* @param action The action to be performed for each element

* @throws NullPointerException if the specified action is null

* @since 1.8

*/

default void forEachRemaining(Consumer super E> action) {

Objects.requireNonNull(action);

while (hasNext())

action.accept(next());

}

}

Iterator接口定义了四个方法以及各个方法的功能,如果有类实现了这个接口,且实现了这些方法,这方法需要实现定义的功能,遵循这些规则:

1).hasNext() 判断容器是否有下一个元素,有则返回true;

2).next() 返回容器中的下一个元素;

3).remove() 移除当前迭代器返回的最后一个元素。这个方法在每次调用next()方法之后只能调用一次;

4).Java 8 增加forEachRemaining方法,它可以实现对余下的所有元素执行指定的操作。

更详细的说明请阅读源码中的注释。

2.ListIterator

ListIterator在Iterator基础上提供了add、set、previous等对列表的操作。但是ListIterator跟Iterator一样,仍是在原列表上进行操作。

ListIterator源码如下:

/**

* An iterator for lists that allows the programmer

* to traverse the list in either direction, modify

* the list during iteration, and obtain the iterator's

* current position in the list. A {@code ListIterator}

* has no current element; its cursor position always

* lies between the element that would be returned by a call

* to {@code previous()} and the element that would be

* returned by a call to {@code next()}.

* An iterator for a list of length {@code n} has {@code n+1} possible

* cursor positions, as illustrated by the carets ({@code ^}) below:

*

* Element(0) Element(1) Element(2) ... Element(n-1)

* cursor positions: ^ ^ ^ ^ ^

*

* Note that the {@link #remove} and {@link #set(Object)} methods are

* not defined in terms of the cursor position; they are defined to

* operate on the last element returned by a call to {@link #next} or

* {@link #previous()}.

*

* This interface is a member of the Java Collections Framework.*/

public interface ListIterator extends Iterator {

// Query Operations

/**

* Returns {@code true} if this list iterator has more elements when

* traversing the list in the forward direction. (In other words,

* returns {@code true} if {@link #next} would return an element rather

* than throwing an exception.)

*

* @return {@code true} if the list iterator has more elements when

* traversing the list in the forward direction

*/

boolean hasNext();

/**

* Returns the next element in the list and advances the cursor position.

* This method may be called repeatedly to iterate through the list,

* or intermixed with calls to {@link #previous} to go back and forth.

* (Note that alternating calls to {@code next} and {@code previous}

* will return the same element repeatedly.)

*

* @return the next element in the list

* @throws NoSuchElementException if the iteration has no next element

*/

E next();

/**

* Returns {@code true} if this list iterator has more elements when

* traversing the list in the reverse direction. (In other words,

* returns {@code true} if {@link #previous} would return an element

* rather than throwing an exception.)

*

* @return {@code true} if the list iterator has more elements when

* traversing the list in the reverse direction

*/

boolean hasPrevious();

/**

* Returns the previous element in the list and moves the cursor

* position backwards. This method may be called repeatedly to

* iterate through the list backwards, or intermixed with calls to

* {@link #next} to go back and forth. (Note that alternating calls

* to {@code next} and {@code previous} will return the same

* element repeatedly.)

*

* @return the previous element in the list

* @throws NoSuchElementException if the iteration has no previous

* element

*/

E previous();

/**

* Returns the index of the element that would be returned by a

* subsequent call to {@link #next}. (Returns list size if the list

* iterator is at the end of the list.)

*

* @return the index of the element that would be returned by a

* subsequent call to {@code next}, or list size if the list

* iterator is at the end of the list

*/

int nextIndex();

/**

* Returns the index of the element that would be returned by a

* subsequent call to {@link #previous}. (Returns -1 if the list

* iterator is at the beginning of the list.)

*

* @return the index of the element that would be returned by a

* subsequent call to {@code previous}, or -1 if the list

* iterator is at the beginning of the list

*/

int previousIndex();

// Modification Operations

/**

* Removes from the list the last element that was returned by {@link

* #next} or {@link #previous} (optional operation). This call can

* only be made once per call to {@code next} or {@code previous}.

* It can be made only if {@link #add} has not been

* called after the last call to {@code next} or {@code previous}.

*

* @throws UnsupportedOperationException if the {@code remove}

* operation is not supported by this list iterator

* @throws IllegalStateException if neither {@code next} nor

* {@code previous} have been called, or {@code remove} or

* {@code add} have been called after the last call to

* {@code next} or {@code previous}

*/

void remove();

/**

* Replaces the last element returned by {@link #next} or

* {@link #previous} with the specified element (optional operation).

* This call can be made only if neither {@link #remove} nor {@link

* #add} have been called after the last call to {@code next} or

* {@code previous}.

*

* @param e the element with which to replace the last element returned by

* {@code next} or {@code previous}

* @throws UnsupportedOperationException if the {@code set} operation

* is not supported by this list iterator

* @throws ClassCastException if the class of the specified element

* prevents it from being added to this list

* @throws IllegalArgumentException if some aspect of the specified

* element prevents it from being added to this list

* @throws IllegalStateException if neither {@code next} nor

* {@code previous} have been called, or {@code remove} or

* {@code add} have been called after the last call to

* {@code next} or {@code previous}

*/

void set(E e);

/**

* Inserts the specified element into the list (optional operation).

* The element is inserted immediately before the element that

* would be returned by {@link #next}, if any, and after the element

* that would be returned by {@link #previous}, if any. (If the

* list contains no elements, the new element becomes the sole element

* on the list.) The new element is inserted before the implicit

* cursor: a subsequent call to {@code next} would be unaffected, and a

* subsequent call to {@code previous} would return the new element.

* (This call increases by one the value that would be returned by a

* call to {@code nextIndex} or {@code previousIndex}.)

*

* @param e the element to insert

* @throws UnsupportedOperationException if the {@code add} method is

* not supported by this list iterator

* @throws ClassCastException if the class of the specified element

* prevents it from being added to this list

* @throws IllegalArgumentException if some aspect of this element

* prevents it from being added to this list

*/

void add(E e);

}

ListIterator的功能更加强大,定义的方法有:

1).hasNext() 向前遍历时,如果有下一个元素返回真;

2).next() 返回下一个元素的值,并将指针加1;

3).hasPrevious() 向相反方向遍历时,如果还有元素返回真;

4).previous() 返回上一个元素的值,并将指针前移1;

5).nextIndex() 返回此时调用next()方法时返回的元素的索引;

6).previousIndex() 返回此时调用previous()方法时返回的元素的索引;

7).remove() 移除最近一次调用next()或previous()方法返回的元素(可选);

8).set(E e) 用元素e将如果此时调用next()或previous()方法返回的元素替换掉;

9).add(E e) 添加元素到此时调用next()返回的元素之前,或此时调用previous()返回的元素之后。

更详细的说明请阅读源码中的注释。

3.Iterator和ListIterator的区别

Iterator和ListIterator的方法对比如下表:

Iterator

ListIterator

hasNext()

hasNext()

覆盖

next()

next()

覆盖

remove()

remove()

覆盖

forEachRemaining(Consumer super E> action)

forEachRemaining(Consumer super E> action)

继承

hasPrevious()

previous()

nextIndex()

previousIndex()

set(E e)

add(E e)

二者的不同之处主要有:

1).Iterator只能单向移动,ListIterator可以双向移动;

2).ListIterator可以删除、替换或添加元素,而Iterator只能删除元素;

3).ListIterator可以返回当前(调用next()或previous()返回的)元素的索引,而Iterator不能。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

java的iterator接口_java Iterator接口和LIstIterator接口分析_java_脚本之家相关推荐

  1. java keytool 代码_JDK keytool证书工具功能代码解析_java_脚本之家

    一.生成证书 keytool -genkey -alias tomcat -keyalg RSA -keystore D:/tomcat.keystore -keypass 123456 -store ...

  2. java的 iterator方法_java iterator方法

    展开全部 iterator方法是JDK提供的迭代32313133353236313431303231363533e4b893e5b19e31333337393536接口进行Java集合的迭代. Ite ...

  3. java工厂到接口_Java基础——接口简单工厂

    声明:本栏目所使用的素材都是凯哥学堂VIP学员所写,学员有权匿名,对文章有最终解释权:凯哥学堂旨在促进VIP学员互相学习的基础上公开笔记. 一.接口 1.接口只做规范和声明不做实现: 2.java中类 ...

  4. java supplier接口_Java函数式接口Supplier接口实例详解

    这篇文章主要介绍了Java函数式接口Supplier接口实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 JDK提供了大量常用的函数式接口以丰 ...

  5. interface接口_Java程序设计--接口interface(笔记)

    有时必须从几个类中派生出一个子类,继承它们所有的属性和方法.但是,Java不支持多重继承.有了接口,就可以得到多重继承的效果. 有时必须从几个类中抽取出一些共同的行为特征,而它们之间又没有继承关系,仅 ...

  6. java可以微信qq同时登陆_java实现 微博登录、微信登录、qq登录实现代码_java_脚本之家...

    微信,微博,QQ,这是现在目前用的最多的手机 APP,我们做产品哪能不跟他们不沾边,对于登录,我想谁也不想要多少个帐号密码,根本记不住! 为了增加用户体验,用户能够快速的注册登录,第三方账号进行登录注 ...

  7. java iterator对象_JAVA Iterator 详解 代码

    Iterator接口 1.所有实现了Collection接口的容器类都有一个Iterator方法用以返回一个实现了Iterator接口的对象 2.Iterator对象称为迭代器,用以方便的操作实现对象 ...

  8. java iterator 倒序_java iterator如何倒序输出

    iterator只能向前迭代 List还额外提供了一个listIterator()方法,该方法返回一个ListIterator对象,ListIterator接口继承了Iterator接口,ListIt ...

  9. java 飞信接口_java 飞信接口

    本接口走的是移动wap飞信接口,绝对安全 package net.duohuo.tengzhinei.Feition; import java.io.BufferedReader; import ja ...

最新文章

  1. HDOJ 6069 素数筛
  2. 《原神》米哈游突然押注脑机接口,CEO:10年内造出10亿人生活的虚拟世界
  3. mvn本地生成jar包放在mvn项目依赖(将jar包传到本地仓库)
  4. Java开发SVM之Eclipse集成LibSVM示例
  5. “Java是编译执行的语言”这句话对吗?
  6. mysql 源代码16384_MySQL源码:Innobase文件系统管理
  7. LinkedHashMap 的理解以及借助其实现LRU
  8. eslint 禁用命令
  9. 光纤收发器按照网管怎么分类?
  10. 一个封装了的选项卡效果js
  11. 解决gensim导入模型报错UnpicklingError: invalid load key, ‘3‘.
  12. 护肤品APP界面设计模板,可以临摹的UI好素材
  13. 调整Virtual Box硬盘大小
  14. selenium之chrome driver版本选择
  15. 微信小程序(十二)uni-app框架开发及组件库
  16. 基于445端口漏洞的入侵测试
  17. php ucenter单点登录,说说ucenter的单点登录
  18. 数据拟合(excel)
  19. word2019如何清除折叠黑色三角形的方法
  20. 弱电机房可视化监控综合管理系统设计方案

热门文章

  1. Git 学习笔记之 merge
  2. 关于之前的函数式编程
  3. ASP.NET MVC5+EF6+EasyUI 后台管理系统(1)-前言与目录(持续更新中...)
  4. psychopy 与脑电打码 eeg
  5. Android界面菜单(4)—快捷菜单
  6. 国内ios分亨组件,
  7. established关键字
  8. vs显示堆栈数据分析_什么是“数据分析堆栈”?
  9. 初创公司怎么做销售数据分析_初创公司与Faang公司的数据科学
  10. 重学TCP协议(9) 半连接队列、全连接队列