Java中Map遍历常用的四种方法
来源:互联网 发布:反域名查询 编辑:程序博客网 时间:2024/05/17 22:44
今天写程序又用到了HashMap(),在此我也总结一下,自己以前用过的四种方法。
- package com.xing.test;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- /**
- *
- * @author Yinxing
- *
- */
- public class OutMap {
- public static void main(String[] args) {
- Map<String, String> map = new HashMap<String, String>();
- map.put("1", "烟台大学");
- map.put("2", "清华大学");
- map.put("3", "北京大学");
- // 第一种:普遍使用,二次取值
- 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
- Java中Map遍历常用的四种方法
- Java中Map遍历常用的四种方法
- java 中map遍历的四种方法和优缺点
- java中遍历map对象的四种方法
- Java中,Map遍历的四种方法
- java中遍历map的四种方法
- Java中遍历Map的四种方法
- java中 遍历 map 的 四种方法
- Java中遍历Map的四种方法总结
- Java中遍历map的四种方法
- java Map 遍历的四种方法
- java 遍历Map的四种方法
- java遍历Map的四种方法
- java 遍历Map的四种方法
- Java 遍历Map的四种方法
- JAVA 遍历Map的四种方法
- Java遍历Map的四种方法
- java 遍历Map的四种方法
- 【笔记(1)】PHP QR Code生成二维码
- 关于usb key和bootstrap的冲突
- [Delphi XE6 FireMonkey ] How to manipulate pixels on a bitmap by scanline property.
- Linux进程间通信方式--本地socket
- Python之禅道
- Java中Map遍历常用的四种方法
- INSERT IGNORE 与INSERT INTO的区别
- 含有指针成员的类的拷贝
- python学习笔记(1)
- 红黑树实现Map
- JAVA实现https单向认证
- 有数字签名的软件就一定值得用户信任吗?
- 程序员生存定律
- android sdk --- dial的用例