如何在 Java 中遍历任意映射?
以下示例使用 Collection 类的 iterator 方法遍历 HashMap。
示例
import java.util.*;
public class Main {
public static void main(String[] args) {
HashMap< String, String> hMap = new HashMap< String, String>();
hMap.put("1", "1st");
hMap.put("2", "2nd");
hMap.put("3", "3rd");
Collection cl = hMap.values();
Iterator itr = cl.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}输出
以上代码示例将产生以下结果。
1st 2nd 3rd
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP