使用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. MySql 生成日期随机数

    select DATE_ADD(sd, INTERVAL FLOOR(1+ RAND() * ((ABS(UNIX_TIMESTAMP(ed) - UNIX_TIMESTAMP(sd))) - 1)) ...

  2. Restful --- 让JSON回归单纯

    设计模式才是软件哲学的根本.. 一种软件架构风格.设计风格,而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓 ...

  3. spring boot之访问静态页面

    楼主前两天自学spring boot,然后在学习的过程中,出现一个疑问,就是如何去访问静态的html网页,这个问题,楼主上网上搜了下,找到的是在resource目录下建立一个templates文件夹, ...

  4. 手工配置oracle数据库

    手工配置Oracle 10G Enterprise Manager今天安装oracle,反复装了几遍都报下面错误:试了几种方法都不行:由于以下错误,Enterprise Manager配置失败启动Da ...

  5. 利用Spring AOP和自定义注解实现日志功能

    Spring AOP的主要功能相信大家都知道,日志记录.权限校验等等. 用法就是定义一个切入点(Pointcut),定义一个通知(Advice),然后设置通知在该切入点上执行的方式(前置.后置.环绕等 ...

  6. e668. 在一组像素中创建缓冲图像

    This example demonstrates how to convert a byte array of pixel values that are indices to a color ta ...

  7. peek函数的用法

    #include <iostream> /* run this program using the console pauser or add your own getch, system ...

  8. (转)windows下编译最新的x264

    二:<windows下编译最新的x264> X264更新的比较快,每天都有更新,但算法模块,基本结构是没有多大变化的.x264都是用C语言写的包括C99,但C99语法是在VC中是没法用的( ...

  9. Javascript农历与公历相互转换

    /**用法 * Lunar.toSolar(2016, 6, 3); 农历转化公历 * Lunar.toLunar(2016, 7, 6); 公历转化农历 */ var Lunar = { MIN_Y ...

  10. 新兵训练营课程——环境与工具Java[转]

    原文地址:http://weibo.com/p/1001643874239169320051 程序员在开发过程中会用到很多工具来提升开发和协作效率,这次介绍的是目前微博平台在开发过程中用到的一些工具, ...