用Iterator实现遍历集合
使用Collection类的Iterator,可以方便的遍历Vector, ArrayList, LinkedList等集合元素,避免通过get()方法遍历时,针对每一种对象单独进行编码。
示例:
- Collection coll = new Vector(); //LinkedList(); //ArrayList();
- coll.add("Tody");
- coll.add("is");
- coll.add("Sunday.");
- // Output all elements by iterator
- Iterator it = coll.iterator();
- while(it.hasNext()) {
- System.out.print(it.next() + " ");
- }
输出:
Tody is Sunday.
1.hasNext()函数的API解释
hasNext
boolean hasNext()
- Returns
trueif the iteration has more elements. (In other words, returnstrueifnext()would return an element rather than throwing an exception.)
- Returns:
trueif the iteration has more elements
---------------------------------------------------------
2.next()函数的API解释
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
- Collection coll = new HashSet();
- coll.add("Tody");
- coll.add("is");
- coll.add("Sunday.");
- // Output all elements by iterator
- Iterator it = coll.iterator();
- while(it.hasNext()) {
- System.out.print(it.next() + " ");
- }
输出:
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实现遍历集合的更多相关文章
- 增强for循环遍历集合或数组
遍历:for循环遍历数组或集合:iterator迭代器遍历集合:还有增强for循环(for each)遍历数组或集合: 遍历数组: 遍历集合:
- 遍历集合的Iterator删除其中的元素
package list; import java.util.LinkedList; /* * 遍历集合的时候删除其中的元素 从后往前删,每次都删除的是最后一个元素,不涉及移位 */public cl ...
- java8 增强的Iterator遍历集合元素
Iterator接口也是Java集合框架的成员,与Collection和Map两个系列的集合不一样的是Collection和Map系列主要用于充当容器的作用,而Iterator正如其名字一样是主要用于 ...
- 如何边遍历集合边删除元素--使用Iterator中的remove()方法
在遍历集合时,想将符合条件的某些元素删除,开始是用了下面的方法 public static void main(String[] args) throws UnsupportedEncodingExc ...
- 迭代器:遍历集合元素的操作. iterator()
package seday11; import java.util.ArrayList;import java.util.Collection;import java.util.Iterator; / ...
- 8.2.2 使用Java8增强的Iterator遍历集合元素
8.2.2 使用Java 8增强的Iterator遍历集合元素 Iterator接口方法 程序示例 Iterator仅用于遍历集合 Iterator必须依附于Collection对象 修改迭代变量的值 ...
- 集合—collection、iterator遍历集合
一.collection接口 1.collection常用方法 点击查看代码 @Test public void test(){ //contains() Collection coll = new ...
- Lambda表达式遍历集合
1.Collection Java 8 为Iterable接口新增了一个forEach(Consumer action)默认方法,该方法所需参数的类型是一个函数式接口,而Iterable接口是Coll ...
- JAVA基础学习之final关键字、遍历集合、日期类对象的使用、Math类对象的使用、Runtime类对象的使用、时间对象Date(两个日期相减)(5)
1.final关键字和.net中的const关键字一样,是常量的修饰符,但是final还可以修饰类.方法.写法规范:常量所有字母都大写,多个单词中间用 "_"连接. 2.遍历集合A ...
随机推荐
- BusyBox编译配置
1. 下载Busybox tar包,如busybox-1.23.0.tar.bz2. 官网:http://www.busybox.net/ 2. make distclean:清除原有配置 ma ...
- scsi线
- Oracle数据误删除的恢复操作
flashbackup 闪回操作: 1. 打开表的闪回功能: alter table dw_stg.fm_user_play_d enable row movement; 2. 查询要闪回的表的记录信 ...
- 常见JVM监控工具用法介绍
VisualVM是一种集成了多个JDK命令行工具的可视化工具,它能为您提供强大的分析能力.所有这些都是免费的!它囊括的命令行工具包括jps,jstat,jmap,jinfo,jstack,JConso ...
- Nginx_lua缓存问题,关闭lua_code_cache
打开nginx.conf配置server{ lua_code_cache off; //关闭lua缓存 重启后生效 server_name localhost; default_type 'text/ ...
- windows和Linux查看端口占用进程并关闭
Windows: 以端口8080为例: 1.查找对应的端口占用的进程:netstat -aon|findstr "8080" ,找到占用8080端口对应的程序的PID号: ...
- iOS边练边学--UINavigationController导航条的使用
一.使用UINavigationController的步骤以及代码 // 程序加载完成后执行的代码 - (BOOL)application:(UIApplication *)application d ...
- 记录github 免登陆用户名密码方式
1.https 代码模式切换为ssh模式: (本博客有文章介绍) 2.~/.ssh/github_rsa.pub 内容添加到github “config“ 目录下面 3.配置~/.ssh/config ...
- DIV内滚动条滚动到指定位置
相对浏览器,将指定div滚到到指定位置,其用法如下: $("html,body").animate({scrollTop: $(obj).offset().top},speed); ...
- jQueryEasyUi行编辑打造增删改查
var $obj;$(function() { $obj = $("#configQueryGrid"); $obj.datagrid({ loadMsg : '数据加载中请稍后… ...