用Iterator实现遍历集合
2014-02-19 10:50
274 查看
使用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()
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
[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 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
[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实现遍历集合
- Java常见集合例题(1、集合的嵌套遍历。2、获取随机数。3三种方式实现对集合中存储对象的遍历)
- Java中Set集合的遍历及实现类比较分析
- Iterator遍历有多级子集合的集合
- 集合遍历之迭代器实现方法
- Java实现Map集合遍历的四种常见方式与用法分析
- 遍历数组和集合可以用增强for语句来实现
- map集合的4种遍历方法和List集合的2种遍历和迭代器Iterator的使用
- 使用<s:iterator>标签来循环遍历某一实体下的set集合数据
- 集合遍历操作:Collection及Iterator接口
- 集合的遍历:Iterator接口、foreach
- Iterator遍历有多级子集合的集合
- 使用Java8增强的Iterator遍历集合元素
- iterator 遍历集合