现在的Java程序员习惯使用Iterator<E>接口或者增强for循环来遍历集合。如果回到JDK 1.0,Enumeration接口则是遍历向量、哈希表的不二之选。本节就解读和翻译一下Enumeration<E>接口的源代码。当然,有一点还是得再强调一下:Enumeration<E>的泛型实在JDK 1.5时加上去的,最初的版本是没有泛型的。

package java.util;

/**
* An object that implements the Enumeration interface generates a
* series of elements, one at a time(一次一个). Successive calls to the
* <code>nextElement</code> method return successive(逐个) elements of the
* series.
* 实现了Enumeration接口的对象自动生成一系列元素,一次一个。
* 逐次调用nextElement()方法,依次返回系列元素。
* <p>
* For example, to print all elements of a <tt>Vector&lt;E&gt;</tt> <i>v</i>:
* 举例而言,打印Vector<V>中的所有元素
* <pre>
* for (Enumeration<E> e = v.elements(); e.hasMoreElements();)
* System.out.println(e.nextElement());</pre>
* <p>
* Methods are provided to enumerate through the elements of a
* vector, the keys of a hashtable, and the values in a hashtable.
* Enumerations are also used to specify the input streams to a
* <code>SequenceInputStream</code>
* <p>
* 所有的方法都被提供来枚举Vector、hashtable中的键、值的元素。
* Enumeration<E>也被用来指定一个到SequenceInputStream的输入流。
*
* 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.
* 提示:Enumeration<E>接口的功能已经被Iterator<E>接口赋复制。除此之外,Iterator<E>接口
* 添加了一个可选删除(元素)操作,以及更加简短的方法名。新的实现应该优先考虑使用Iterator<E>
* 接口,而非Enumeration<E>。
*
* @see java.util.Iterator
* @see java.io.SequenceInputStream
* @see java.util.Enumeration#nextElement()
* @see java.util.Hashtable
* @see java.util.Hashtable#elements()
* @see java.util.Hashtable#keys()
* @see java.util.Vector
* @see java.util.Vector#elements()
*
* @author Lee Boynton
* @since JDK1.0 历史遗留问题
*/
public interface Enumeration<E> {
/**
* Tests if this enumeration contains more elements.
* 测试是否此枚举还包含有元素。
* @return <code>true</code> if and only if this enumeration object
* contains at least one more element to provide;
* <code>false</code> otherwise.
* 当且仅当此枚举对象还有至少一个元素可以提供,返回true;否则,返回false。
*/
boolean hasMoreElements(); // 别少了s,老外写代码是很重视单复数滴 /**
* Returns the next element of this enumeration if this enumeration
* object has at least one more element to provide.
* 如果此枚举对象还有至少一个元素可以提供,那么返回此枚举的下一个元素。
* @return the next element of this enumeration.
* 返回此枚举的下一个元素
* @exception NoSuchElementException if no more elements exist.
* 如果没有更多的元素存在,抛出NoSuchElementException异常
*/
E nextElement();
}

阅读完Enumeration<E>的源代码和翻译之后,想必您和我都有同一个想法:这玩意并没有什么*用。

那么为什么我们还要去了解这个接口的源代码呢?作用就是、、、能够装逼。

回归JDK源代码(2)Enumeration<E>接口的更多相关文章

  1. 通过分析 JDK 源代码研究 TreeMap 红黑树算法实现

    本文转载自http://www.ibm.com/developerworks/cn/java/j-lo-tree/ 目录: TreeSet 和 TreeMap 的关系 TreeMap 的添加节点 Tr ...

  2. JDK源代码学习系列04----ArrayList

                                                                             JDK源代码学习系列04----ArrayList 1 ...

  3. JDK源代码学习系列05----LinkedList

                                             JDK源代码学习系列05----LinkedList 1.LinkedList简单介绍 LinkedList是基于双向 ...

  4. 调试JDK源代码-一步一步看HashMap怎么Hash和扩容

    调试JDK源代码-一步一步看HashMap怎么Hash和扩容 调试JDK源代码-ConcurrentHashMap实现原理 调试JDK源代码-HashSet实现原理 调试JDK源代码-调试JDK源代码 ...

  5. JDK源代码学习系列03----StringBuffer+StringBuilder

                         JDK源代码学习系列03----StringBuffer+StringBuilder 因为前面学习了StringBuffer和StringBuilder的父类 ...

  6. 无法debug断点跟踪JDK源代码——missing line number attributes的解决方法

    在项目工程->Properties->Java Build Path->Libraries中导入的JRE System Library库里,给jar包添加JDK源代码包后,能够直接打 ...

  7. JDK源代码学习系列07----Stack

                                                                   JDK源代码学习系列07----Stack 1.Stack源代码很easy ...

  8. eclipse Mars查看JDK源代码

    eclipse Mars查看JDK源代码 问题描写叙述,eclipse(mars)下看不到JDK类的声明即源代码部分的内容. 如图右击string类型: 点击打开声明.结果出现了下图所看到的的错误,无 ...

  9. 如何调试JDK源代码并查看局部变量值

    如下图: 按F5进入String.startsWith,如下: 点“Edit Source Lookup Path” 附加源代码,如下图: 附加上源代码后如下: 可以看到,当鼠标放在“prefix”上 ...

随机推荐

  1. kuangbin专题七 HDU1754 I Hate It (单点修改维护最大值)

    很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老师有 ...

  2. element,点击查看,实现tab切换:

    点击查看,实现tab切换: 代码如下: <template> <div> <el-table :data="tableData" style=&quo ...

  3. spring 配置 junit

    package cn.hefen.mall.app; import cn.hefen.mall.app.model.ResultMap; import cn.hefen.mall.app.model. ...

  4. liunx 内置mail 发送邮件

    邮件配置文件/etc/mail.rc [root@ ~]# vim /etc/mail.rc #添加.修改如下内容 .com .com .com set smtp-auth-password=xxx ...

  5. java中检测网络是否相通

    转载:https://www.cnblogs.com/moon-jiajun/p/3454576.html 1 package com.cjj.client; 2 3 import java.io.I ...

  6. mybatis mapper问题列表

    id出现两次 2018-11-14 16:15:03.833 DEBUG 41432 --- [           main] c.a.i.o.d.mapper.DatvMapper.insert  ...

  7. centos7安装与卸载软件

    安装 yum install 服务名 查看服务名 rpm -qa |grep -i aerospike 或者 yum list installed | grep aerospike 卸载 yum re ...

  8. Reactive Extensions入门

    https://www.cnblogs.com/yangecnu/archive/2012/11/03/Introducting_ReactiveExtensions.html 前面我写过7篇文章粗略 ...

  9. 查看linux系统各种参数配置的命令

    查看linux系统各种参数配置的命令 last |grep shutdown                             //查看上次关机时间 last |grep reboot     ...

  10. 转 如何在secureCRT上设置常用的快捷输出按钮栏听语音

    https://jingyan.baidu.com/article/5d6edee2f32de199eadeec25.html 要注意secureCRT的版本,建议下载最新版本的软件         ...