用Iterator实现遍历集合
2013-07-25 15:34
253 查看
使用Collection类的Iterator,可以方便的遍历Vector, ArrayList, LinkedList等集合元素,避免通过get()方法遍历时,针对每一种对象单独进行编码。
示例:
[java] view plaincopy
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解释
boolean java.util.Iterator.hasNext()
Returns
Returns:
---------------------------------------------------------
2.next()函数的API解释
Object java.util.Iterator.next()
Returns the next element in the iteration.
Returns:the next element in the iterationThrows:
[java] view plaincopy
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。
示例:
[java] view plaincopy
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解释
boolean java.util.Iterator.hasNext()
hasNext
boolean hasNext()
Returns
trueif the iteration has more elements. (In other words, returns
trueif
next()would return an element rather than throwing an exception.)
Returns:
trueif 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 iterationThrows:
NoSuchElementException- if the iteration has no more elements
[java] view plaincopy
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。
相关文章推荐
- 用Iterator实现遍历集合
- 用Iterator实现遍历集合
- 用Iterator实现遍历集合
- 用Iterator模式实现遍历集合
- 用Iterator实现遍历集合
- 用Iterator实现遍历集合
- 用Iterator实现遍历集合
- 集合框架-for、foreach、Iterator三种遍历方法
- Java Iterator遍历List集合
- 黑马程序员------集合的遍历Iterator/ListIterator
- 利用map遍历器Iterator遍历map集合的实例。
- for循环 增强for循环 iterator遍历集合的区别与用法
- 在JSP中用自定义标签实现集合的遍历——山寨版c:foreach
- 如何在java中实现List集合的遍历
- Java遍历集合的几种方法分析(实现原理、算法性能、适用场合)
- JAVA提高篇(31)--Iterator和ListIterator 迭代器的使用(遍历集合)
- Java遍历集合的几种方法分析(实现原理、算法性能、适用场合)
- 删除集合中的某元素,边遍历边删除使用Iterator
- 黑马程序员——使用Iterator遍历集合
- collections 接口存储一组不唯一,无序的对象。提供了对集合进行排序,遍历等对种算法的实现