遍历Hashmap的四种方式
来源:互联网 发布:网络上有重名上不去网 编辑:程序博客网 时间:2024/05/16 17:47
HashMap也是我们使用非常多的Collection,它是基于哈希表的 Map 接口的实现,以key-value的形式存在。在HashMap中,key-value总是会当做一个整体来处理,系统会根据hash算法来来计算key-value的存储位置,我们总是可以通过key快速地存、取value。
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"); //第一种:普遍使用,二次取值 System.out.println("通过Map.keySet遍历key和value:"); for (String key : map.keySet()) { System.out.println("key= "+ key + " and value= " + map.get(key)); } //第二种 System.out.println("通过Map.entrySet使用iterator遍历key和value:"); Iterator<Map.Entry<String, String>> it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, String> entry = it.next(); System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); } //第三种:推荐,尤其是容量大时 System.out.println("通过Map.entrySet遍历key和value"); for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); } //第四种 System.out.println("通过Map.values()遍历所有的value,但不能遍历key"); for (String v : map.values()) { System.out.println("value= " + v); } }
阅读全文
0 0
- HashMap的四种循环遍历方式
- 遍历HashMap的四种方式
- HashMap的四种遍历方式
- Java -- HashMap的四种遍历方式
- 遍历HashMap的四种方式
- 遍历Hashmap的四种方式
- hashmap四种遍历方式
- Java中HashMap遍历的四种方式
- Java中HashMap遍历的四种方式
- Java中HashMap遍历的四种方式
- Java中HashMap遍历的四种方式
- HashMap的四种遍历
- hashMap的四种遍历
- 遍历HashMap的四种
- HashMap的遍历方式
- HashMap的遍历方式
- HashMap的遍历方式
- 遍历HashMap的四种方法
- 安装SQL Server 2008 报错“此计算机上安装了 Microsoft Visual Studio 2008 的早期版本”解决方法
- 【博弈】Alice and Bob CodeForces
- C++学习笔记 5th —— 万能流程控制if语句 续篇:扩展与实际应用
- Linux USB RNDIS设备在WinXP、Win7、Win10下的驱动安装
- 转:oracle connect by level start with使用
- 遍历Hashmap的四种方式
- 全球互联网技术大会GITC2017再次走进上海!
- 常用数据类型取值范围
- Kerberos简介
- 堆排序
- 安装vmware-tools出现"what is the location of the "ifconfig"program on your machine?"
- Intellij idea 的maven项目如何通过maven自动下载jar包
- eclipse中插件windowBuilder的使用
- php关键字区分