使用Collection类的Iterator,可以方便的遍历Vector, ArrayList, LinkedList等集合元素,避免通过get()方法遍历时,针对每一种对象单独进行编码。

示例:

  1. Collection coll = new Vector(); //LinkedList(); //ArrayList();
  2. coll.add("Tody");
  3. coll.add("is");
  4. coll.add("Sunday.");
  5. // Output all elements by iterator
  6. Iterator it = coll.iterator();
  7. while(it.hasNext()) {
  8. System.out.print(it.next() + " ");
  9. }

输出:

Tody is Sunday.

1.hasNext()函数的API解释

boolean java.util.Iterator.hasNext()

hasNext

boolean hasNext()
Returns true if the iteration has more elements. (In other words, returns true if next() would return an element rather than throwing an exception.)

Returns:
true if the iteration has more elements

---------------------------------------------------------

2.next()函数的API解释

Object java.util.Iterator.next()

next

E next()
Returns the next element in the iteration.

Returns:
the next element in the iteration
Throws:
NoSuchElementException - if the iteration has no more elements
  1. Collection coll = new HashSet();
  2. coll.add("Tody");
  3. coll.add("is");
  4. coll.add("Sunday.");
  5. // Output all elements by iterator
  6. Iterator it = coll.iterator();
  7. while(it.hasNext()) {
  8. System.out.print(it.next() + " ");
  9. }

输出:

is Sunday. Tody

由上面两个例子看出,在List和Set对象中,Iterator的next()方法返回的值是不一样的。

原因是List属于线性集合,元素是有序的,读取时是按照数组的形式,一个接一个的读取,存储也是按照add的顺序添加的。

而Set属于非线性的,是无序的,所以读取的元素与添加的顺序不一定一致。

对于HashSet,其实它返回的顺序是按Hashcode的顺序。

如果迭代也有序,则可以用LinkedHashSet。

http://topic.csdn.net/u/20101227/09/63a23d05-7f15-4b0e-9287-e97f96ba4349.html?77188351

用Iterator实现遍历集合的更多相关文章

  1. 增强for循环遍历集合或数组

    遍历:for循环遍历数组或集合:iterator迭代器遍历集合:还有增强for循环(for each)遍历数组或集合: 遍历数组: 遍历集合:

  2. 遍历集合的Iterator删除其中的元素

    package list; import java.util.LinkedList; /* * 遍历集合的时候删除其中的元素 从后往前删,每次都删除的是最后一个元素,不涉及移位 */public cl ...

  3. java8 增强的Iterator遍历集合元素

    Iterator接口也是Java集合框架的成员,与Collection和Map两个系列的集合不一样的是Collection和Map系列主要用于充当容器的作用,而Iterator正如其名字一样是主要用于 ...

  4. 如何边遍历集合边删除元素--使用Iterator中的remove()方法

    在遍历集合时,想将符合条件的某些元素删除,开始是用了下面的方法 public static void main(String[] args) throws UnsupportedEncodingExc ...

  5. 迭代器:遍历集合元素的操作. iterator()

    package seday11; import java.util.ArrayList;import java.util.Collection;import java.util.Iterator; / ...

  6. 8.2.2 使用Java8增强的Iterator遍历集合元素

    8.2.2 使用Java 8增强的Iterator遍历集合元素 Iterator接口方法 程序示例 Iterator仅用于遍历集合 Iterator必须依附于Collection对象 修改迭代变量的值 ...

  7. 集合—collection、iterator遍历集合

    一.collection接口 1.collection常用方法 点击查看代码 @Test public void test(){ //contains() Collection coll = new ...

  8. Lambda表达式遍历集合

    1.Collection Java 8 为Iterable接口新增了一个forEach(Consumer action)默认方法,该方法所需参数的类型是一个函数式接口,而Iterable接口是Coll ...

  9. JAVA基础学习之final关键字、遍历集合、日期类对象的使用、Math类对象的使用、Runtime类对象的使用、时间对象Date(两个日期相减)(5)

    1.final关键字和.net中的const关键字一样,是常量的修饰符,但是final还可以修饰类.方法.写法规范:常量所有字母都大写,多个单词中间用 "_"连接. 2.遍历集合A ...

随机推荐

  1. BUS Matrix

    ARM的BUS Matrix就是多主(Core,DMA等).多从(内部RAM,APB,外部总线等)的交联和仲裁.目的是为了提高不同主机访问不同外设情况下的带宽,另外一个就是简化Bus Master的协 ...

  2. TCP/IP状态详解[转]

    TCP正常建立和关闭的状态变化     TCP连接的建立可以简单的称为三次握手,而连接的中止则可以叫做 四次握手.   建立连接   在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建 ...

  3. 一站式学习Wireshark(一):Wireshark基本用法

    按照国际惯例,从最基本的说起. 抓取报文: 下载和安装好Wireshark之后,启动Wireshark并且在接口列表中选择接口名,然后开始在此接口上抓包.例如,如果想要在无线网络上抓取流量,点击无线接 ...

  4. kafka操作清单

    1. 查看topic个数 ./kafka-topics.,dwtest-data3:,dwtest-data4: 2. 查看topic的生产者与消费者消息 ./kafka-topics.,dwtest ...

  5. linux 使用NSF 映射远程磁盘目录

    假设源目录在192.168.1.1机器上,目录为/data 客户端集群在192.168.1.2, 需要将192.168.1.1机器上的/data目录到本地的/data目录 1.在两台机器上安装nsf ...

  6. Cygwin镜像使用帮助

    Cygwin镜像使用帮助 收录架构 x86 x86_64 收录版本 所有版本 更新时间 每12小时更新一次   使用说明 选择从互联网安装, 在"User URL"处输入以下地址 ...

  7. 《FPGA全程进阶---实战演练》第三章之PCB叠层

    1.双面板 在双层板设计layout时,最好不要不成梳状结构,因为这样构成的电路,回路面积较大,但是只要对较重要的信号加以地保护,布线完成之后将空的地方敷上地铜皮,并在多个过孔将两个地连接起来,可以弥 ...

  8. Graying the black box: Understanding DQNs

    Zahavy, Tom, Nir Ben-Zrihem, and Shie Mannor. "Graying the black box: Understanding DQNs." ...

  9. (转)windows平台时间函数性能比较QueryPerformanceCounter,GetTickCount,ftime,time,GetLocalTime,GetSystemTimeAsFileTime

    执行 10000000 次, 耗时 2258,369 微秒     QueryPerformanceCounter 执行 10000000 次, 耗时 26,347 微秒    GetTickCoun ...

  10. php header函数下载文件实现代码

    在php中header函数的使用很大,header不但可以向客户端发送原始的 HTTP 报头信息,同时还可以直接实现文件下载操作 header函数最常用的不是用于下载而是用于发送http类的 跳转 它 ...