迭代器为我们提供了统一的遍历容器的方式,参见以下示例代码:
【示例】迭代器遍历List
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class Test {
public static void main(String[] args) {
List<String> aList = new ArrayList<String>();
for ( int i = 0 ; i < 5 ; i++) {
aList.add( "a" + i);
}
System.out.println(aList);
for (Iterator<String> iter = aList.iterator(); iter.hasNext();) {
String temp = iter.next();
System.out.print(temp + "\t" );
if (temp.endsWith( "3" )) { // 删除3结尾的字符串
iter.remove();
}
}
System.out.println();
System.out.println(aList);
}
} |
执行结果如图所示:
老鸟建议
如果遇到遍历容器时,判断删除元素的情况,使用迭代器遍历!
【示例】迭代器遍历Set
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class Test {
public static void main(String[] args) {
Set<String> set = new HashSet<String>();
for ( int i = 0 ; i < 5 ; i++) {
set.add( "a" + i);
}
System.out.println(set);
for (Iterator<String> iter = set.iterator(); iter.hasNext();) {
String temp = iter.next();
System.out.print(temp + "\t" );
}
System.out.println();
System.out.println(set);
}
} |
执行结果如图所示:
【示例】迭代器遍历Map一
1
2
3
4
5
6
7
8
9
10
11
12
|
public class Test {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put( "A" , "高淇" );
map.put( "B" , "高小七" );
Set<Entry<String, String>> ss = map.entrySet();
for (Iterator<Entry<String, String>> iterator = ss.iterator(); iterator.hasNext();) {
Entry<String, String> e = iterator.next();
System.out.println(e.getKey() + "--" + e.getValue());
}
}
} |
执行结果如图所示:
我们也可以通过map的keySet()、valueSet()获得key和value的集合,从而遍历它们。
【示例】迭代器遍历Map二
1
2
3
4
5
6
7
8
9
10
11
12
|
public class Test {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put( "A" , "高淇" );
map.put( "B" , "高小七" );
Set<String> ss = map.keySet();
for (Iterator<String> iterator = ss.iterator(); iterator.hasNext();) {
String key = iterator.next();
System.out.println(key + "--" + map.get(key));
}
}
} |
执行结果如图所示: