转至:https://www.cnblogs.com/Berryxiong/p/6144086.html
public static void main(String[] args) {
Map<String,String> map=new HashMap<String,String>();
map.put("1", "value1");
map.put("2", "value2");
map.put("3", "value3");
map.put("4", "value4"); //第一种:普通使用,二次取值
System.out.println("\n通过Map.keySet遍历key和value:");
for(Object key:map.keySet())
{
System.out.println("Key: "+key+" Value: "+map.get(key));
} //第二种
System.out.println("\n通过Map.entrySet使用iterator遍历key和value: ");
Iterator map1it=map.entrySet().iterator();
while(map1it.hasNext())
{
      // 一定是 Map.Entry
Map.Entry<String, String> entry=(Entry<String, String>) map1it.next();
System.out.println("Key: "+entry.getKey()+" Value: "+entry.getValue());
} //第三种:推荐,尤其是容量大时
System.out.println("\n通过Map.entrySet遍历key和value");
for(Map.Entry<String, String> entry: map.entrySet())
{
System.out.println("Key: "+ entry.getKey()+ " Value: "+entry.getValue());
} //第四种
System.out.println("\n通过Map.values()遍历所有的value,但不能遍历key");
for(String v:map.values())
{
System.out.println("The value is "+v);
}
}

遍历HashMap的四种方式的更多相关文章

  1. java 遍历Map的四种方式

      java 遍历Map的四种方式 CreationTime--2018年7月16日16点15分 Author:Marydon 一.迭代key&value 第一种方式:迭代entrySet 1 ...

  2. Java中遍历Map的四种方式

    Demo如下 Map<String, String> map = new HashMap<>(); map.put("key1","data1&q ...

  3. java遍历map的四种方式

    在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...

  4. 遍历Map的四种方式

    方法一 在for-each循环中使用entries来遍历 这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. 注意:for-each循环在java 5中被引入所以该方法只能应用于 ...

  5. 遍历HashMap的四种方法

    public static void main(String[] args) { Map<String,String> map=new HashMap<String,String&g ...

  6. 遍历hashmap 的四种方法

    以下列出四种方法 public static void main(String[] args) { Map<String,String> map=new HashMap<String ...

  7. 遍历hashMap的两种方式

    第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Ma ...

  8. Java中遍历ConcurrentHashMap的四种方式

    //方式一:在for-each循环中使用entries来遍历 System.out.println("方式一:在for-each循环中使用entries来遍历"); for(Map ...

  9. 遍历Map的四种方式(Java)

    public static void main(String[] args) { Map<String, String> map = new HashMap<String, Stri ...

随机推荐

  1. Flask python初期九九乘法表

    from  flask import Flask    #导入 app = Flask(__name__) @app.route('/') def index(): res=" " ...

  2. 解决flutter的image_cropper组件引入报错问题

    在使用flutter的图片裁剪组件image_cropper,github:https://github.com/hnvn/flutter_image_cropper 根据它的要求,安卓需要在文件[A ...

  3. Ubuntu:命令行下浏览网页

    前述 兴起,试一下不用图形化界面浏览 安装w3m 直接进入root账号 apt-get install w3m 检验是否成功 w3m www.baidu.com 就这样成功的进入baidu了,纯文本模 ...

  4. 学习ASP.NET Core Razor 编程系列十二——在页面中增加校验

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

  5. 机器学习之决策树三-CART原理与代码实现

    决策树系列三—CART原理与代码实现 本文系作者原创,转载请注明出处:https://www.cnblogs.com/further-further-further/p/9482885.html ID ...

  6. 是时候给大家介绍 Spring Boot/Cloud 背后豪华的研发团队了。

    看了 Pivotal 公司的发展历史,这尼玛就是一场商业大片呀. 我们刚开始学习 Spring Boot 的时候肯定都会看到这么一句话: Spring Boot 是由 Pivotal 团队提供的全新框 ...

  7. 4.1ASP.NET Core请求过程「深入浅出ASP.NET Core系列」

    希望给你3-5分钟的碎片化学习,可能是坐地铁.等公交,积少成多,水滴石穿,谢谢关注. HTTP请求过程 这里展示整体的HTTP请求的过程,这里化繁为简,保留了主干流程: 从浏览器输入域名开始,这里忽略 ...

  8. Python编程从入门到实践笔记——文件

    Python编程从入门到实践笔记——文件 #coding=gbk #Python编程从入门到实践笔记——文件 #10.1从文件中读取数据 #1.读取整个文件 file_name = 'pi_digit ...

  9. Writing a Simple Publisher and Subscriber

    用c++实现一个publisher/subscriber publisher #include "ros/ros.h" #include "std_msgs/String ...

  10. Autofac 和 Quartz.Net 自动注入的整合

    一:问题场景 在一次项目开发中,项目中已使用了Autofac.在新需求中要用到Quatrz.Net.在任务中使用注入方法,确始终无法使用注入的方法,经过千百次的度娘,终于找到了解决办法!吐槽下度娘真心 ...