Java中的迭代迭代器Iterator与枚举器Enumeration
Iterator 和 Enumeration区别
Iterator 和 Eumberation都是Collection集合的遍历接口,我们先看下他们的源码接口
package java.util;
public interface Enumeration<E> {
boolean hasMoreElements();
E nextElement();
}
package java.util;
public interface Iterator<E> {
boolean hasNext();
E next();
void remove();
}
1 引入的时间不同
Iteration是从JDK1.0 开始引入,Enumeration是从JDK1.2开始引入
2 接口不同
Enumeration只有2个函数接口。通过Enumeration,我们只能读取集合的数据,而不能对数据进行修改
Iteration只有3个函数的接口。Iteration除了能读取集合的数据之外,也能对数据进行删除
Java的API 解析里面对两个接口的不同是这样解释的:
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.
The bottom line is, both Enumeration and Iterator will give successive elements, but Iterator is improved in such a way so the method names are shorter, and has an additional remove method. Here is a side-by-side comparison:
Enumeration Iterator
---------------- ----------------
hasMoreElement() hasNext()
nextElement() next()
N/A remove()
3 Enumeration是历史遗留接口
Enumeration接口作为一个遗留接口主要用来实现例如 Vextor HashTable Stack 的遍历,而Iterator作为比较新的接口,实现了Collection框架下大部分实现类的遍历比如说 ArrayList LinkedList HashSet LinkedHashSet TreeSet HashMap LinkedHashMap TreeMap 等等
4 Fail-Fast Vs Fail-Safe
Iterator支持Fail-Fast机制,当一个线程在遍历时,不允许另外一个线程对数据进行修改(除非调用实现了Iterator的Remove方法)。、
因此Iterator被认为是安全可靠的遍历方式
5、应该使用哪个
还是摘一段JAVA API解析上的一段话(大意就是虽然两个接口的功能是重复的,但是Iterator由于比Enumeration多了remove方法且接口方法名字更短,因此推荐在后面的类中使用Iterator)
According to Java API Docs, Iterator is always preferred over the Enumeration. Here is the note from the Enumeration Docs.
NOTE: The functionality of this interface is duplicated by the Iterator interface. In addition, Iterator adds an optional remove operation,
and has shorter method names. New implementations should consider using Iterator in preference to Enumeration.
Java中的迭代迭代器Iterator与枚举器Enumeration的更多相关文章
- ruby迭代器iterator和枚举器Enumerator
编写自定义的迭代器 The defining feature of an iterator method is that it invokes a block of code associatedwi ...
- Java中的Iterable与Iterator详解
在Java中,我们可以对List集合进行如下几种方式的遍历: List<Integer> list = new ArrayList<>(); list.add(5); list ...
- Java中的集合迭代器
集合的迭代器 任何集合都有迭代器. 任何集合类,都必须能以某种方式存取元素,否则这个集合容器就没有任何意义. 迭代器,也是一种模式(也叫迭代器模式).在java中它是一个对象,其目的是遍历并选中其中的 ...
- Java基础学习(五)-- Java中常用的工具类、枚举、Java中的单例模式之详解
Java中的常用类 1.Math : 位于java.lang包中 (1)Math.PI:返回一个最接近圆周率的 (2)Math.abs(-10):返回一个数的绝对值 (3)Math.cbrt(27): ...
- java中过滤器(Filter)与拦截器(Interceptor )区别
过滤器(Filter) Servlet中的过滤器Filter是实现了javax.servlet.Filter接口的服务器端程序,主要的用途是设置字符集.控制权限.控制转向.做一些业务逻辑判断等.其工作 ...
- java 中的迭代
package cn.zhou.com; import java.util.ArrayList; import java.util.Collection; import java.util.Itera ...
- 沉淀再出发:java中的HashMap、ConcurrentHashMap和Hashtable的认识
沉淀再出发:java中的HashMap.ConcurrentHashMap和Hashtable的认识 一.前言 很多知识在学习或者使用了之后总是会忘记的,但是如果把这些只是背后的原理理解了,并且记忆下 ...
- java 中,for、for-each、iterator 区别
java 中,for.for-each.iterator 区别: 无论是在数组中还是在集合中,for-Each加强型for循环都是它们各自的普通for循环的一种"简写方式",即两者 ...
- 重学 Java 设计模式:实战迭代器模式「模拟公司组织架构树结构关系,深度迭代遍历人员信息输出场景」
作者:小傅哥 博客:https://bugstack.cn - 原创系列专题文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 相信相信的力量! 从懵懂的少年,到拿起键盘,可以写一个Hell ...
随机推荐
- [LeetCode] 1.Two Sum 两数之和分析以及实现 (golang)
题目描述: /* Given an array of integers, return indices of the two numbers such that they add up to a sp ...
- 聊聊、Zookeeper Linux 集群服务
今天是平安夜,先祝大家平安夜快乐.这篇文章我们来谈谈 Zookeeper Linux 集群. 为什么要集群呢?因为一台服务不够.集群是为了系统扩容,系统稳定.一台服务挂了,没关系,我还有其他的服务.集 ...
- 解决 ultraedit 菜单字体模糊
新装完ultraedit后,使用时菜单里的字体非常小,模糊不清非常不爽. 1.安装好UltraEdit后,到安装目录里,复制一份Uedit32.exe文件用于修改.2.使用UltraEdit打开复制的 ...
- hdu1061(C++)
简单的找规律,不妨设N=10*x+a(a=N%10),那么N^N=(10*x+a)^N,用二项式展开定理可以知道N^N%10=a^N%10; 由于0<a<10,打表a^1,a^2,a^3, ...
- ylb:SQL 索引(Index)
ylbtech-SQL Server: SQL Server-SQL 索引(Index) SQL 索引(Index). ylb:索引(Index) 返回顶部 --=================== ...
- nodejs session 设计
会话管理 { //保存会话 _data : {}, /** 会话基本操作 ***/ //查找会话 getSession : function(id){}, //创建会话 createSession : ...
- SSO单点登录系列1:cas客户端源码分析cas-client-java-2.1.1.jar
落雨 cas 单点登录 希望能给以后来研究cas的兄弟留下一点思路,也算是研究了两天的成果,外国人的代码写的很晦涩,翻译下来也没有时间继续跟进,所以有错误的还请大家跟帖和我讨论,qq 39426378 ...
- 【剑指Offer学习】【面试题37:两个链表的第一个公共结点】
题目:输入两个链表,找出它们的第一个公共结点. 链表结点定义 /** * 链表结点类 */ private static class ListNode { int val; ListNode next ...
- 成都传智播客Java/PHP培训就业率高
依据传智播客的数据统计,传智播客的学员有五分之中的一个的能在毕业前找到惬意的工作,一半的学员能在毕业后一个月之内找到惬意的工作,一般在毕业后两个月之内绝大多数同学都能找到惬意的工作.而且传智播客毕业学 ...
- CSS之BFC
BFC(Block Formatting Context,块格式上下文) 具有BFC特性的元素能够看作是隔离了的独立容器,容器里面的元素不会在布局上影响到外面的元素. 在CSS3中.BFC叫做Flow ...