使用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. TP5 display()

    tp3.x $this->display(); tp5 return $this->fetch();

  2. 受打击了:你是学.net 的吧?

    我在网上投了简历,今天去面试, 去到才知道有面试题做,做完之后自知答的很烂. 没想到面试我的那个人,一开始就很直接,说: 我感觉你很喜欢用英语, 但英语很烂 我觉得你很喜欢用别人的东西, 但技术水平很 ...

  3. .net提供的5种request-response方法一

    .net提供了三种基本方法和两种底层方法来发送http请求和接收http响应,通过这些方法,我们可以模仿在浏览器地址栏输入URL地址访问网页的方法.我们发送http请求,接收服务器返回的响应(通常就是 ...

  4. DirectoryEntry 账户启动与停用 以及创建账户等

    启动账户: DirectoryEntry usr = new DirectoryEntry("LDAP://CN=New User,CN=users,DC=fabrikam,DC=com&q ...

  5. 使用 const 提高函数的健壮性

    使用 const  提高函数的健壮性 看到 const 关键字,C++程序员首先想到的可能是 const 常量.这可不是良好的条件 反射.如果只知道用 const 定义常量,那么相当于把火药仅用于制作 ...

  6. 嵌入式驱动开发之usb 无线网卡驱动---RT2870STA dm368

    RT2870STA 368的无线网卡驱动! http://www.linuxidc.com/Linux/2014-02/96979.htm

  7. CentOS 7修改MySQL 5.6字符集为UTF-8

    MySQL编码原因会导致数据库出现中文乱码 解决办法: 修改MySQL数据库字符编码为UTF-8,UTF-8包含全世界所有国家需要用到的字符,是国际编码. 具体操作: 1.进入MySQL控制台 mys ...

  8. ZABBIX API简介及使用

    API简介 Zabbix API开始扮演着越来越重要的角色,尤其是在集成第三方软件和自动化日常任务时.很难想象管理数千台服务器而没有自动化是多么的困难.Zabbix API为批量操作.第三方软件集成以 ...

  9. 查看Centos系统最近一次启动时间和运行时间

    1.uptime命令 [spark@Master Log_Data]$ uptime 09:18:01 up 20:17,  1 user,  load average: 0.13, 0.12, 0. ...

  10. js 查找指定函数的内容

    function test(){  //hahahhahahhahahha }alert(test.toString());