Java 实例 - HashMap遍历

3514 1
LZH实名认证 2022-4-1 20:21:09 | 显示全部楼层 |阅读模式
以下实例演示了如何使用 Collection 类的 iterator() 方法来遍历集合:

Main.java 文件
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
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行