Enumeration, Iterator and ListIterator in Java
2016-07-14 12:58
591 查看
Both Enumeration and Iterator are interfaces in Java for getting successive elements. Enumeration is older while iterator was introduced later with some improvements. So it is preferred to use Iterator, but we will see Enumeration as well quickly.
Enumeration Vs Iterator In Java
Enumeration interface
Enumeration interface has two methods:
Iterator interface
Iterators differ from enumerations by adding an optional remove operation, and having shorter method names.
Methods of Iterator are:
Iterator is the super interface for the Collection interface.
The Iterable interface with only one method for returning an Iterator.
Iterator is considered more secure and safe because it does not allow other thread to modify the collection object while some thread is iterating over it and throws ConcurrentModificationException in such cases.
Iterators vs. Enumerations
Iterator has taken the place of Enumeration in the Java collections framework.
Iterator is considered more secure and safe.
Java Specification recommends new implementations to consider using Iterator in preference to Enumeration as the functionality of Enumeration interface is duplicated by the Iterator interface with major improvements.
Enumerations might be faster compared to Iterators due to their read only nature. Still it would be better to go with Iterators as it is the Java recommendation. StringTokenizer is one method in Java that still uses Enumeration.
ListIterator
ListIterator is a sub-interface of Iterator.
ListIterator 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 ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next().
The java.util.List has a listIterator() method that returns a listIterator over the elements in this list (in proper sequence).
Methods of a ListIterator are:
Note that E is a generic type and any type can be specified.
Enumeration Vs Iterator In Java
Enumeration interface
Enumeration interface has two methods:
hasMoreElements() nextElement()
Iterator interface
Iterators differ from enumerations by adding an optional remove operation, and having shorter method names.
Methods of Iterator are:
hasNext() next() remove()
Iterator is the super interface for the Collection interface.
The Iterable interface with only one method for returning an Iterator.
Iterator is considered more secure and safe because it does not allow other thread to modify the collection object while some thread is iterating over it and throws ConcurrentModificationException in such cases.
Iterators vs. Enumerations
Iterator has taken the place of Enumeration in the Java collections framework.
Iterator is considered more secure and safe.
Java Specification recommends new implementations to consider using Iterator in preference to Enumeration as the functionality of Enumeration interface is duplicated by the Iterator interface with major improvements.
Enumerations might be faster compared to Iterators due to their read only nature. Still it would be better to go with Iterators as it is the Java recommendation. StringTokenizer is one method in Java that still uses Enumeration.
ListIterator
ListIterator is a sub-interface of Iterator.
ListIterator 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 ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next().
The java.util.List has a listIterator() method that returns a listIterator over the elements in this list (in proper sequence).
Methods of a ListIterator are:
add(E e) hasNext() hasPrevious() next(), returns E nextIndex() previous(), returns E previousIndex() remove() set(E e)
Note that E is a generic type and any type can be specified.
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树