遍历HashMap的四种方法
来源:互联网 发布:java attach source 编辑:程序博客网 时间:2024/05/16 13:42
关于HashMap的遍历方式:
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遍历的四种方法
- 遍历HashMap的四种方法
- 遍历HashMap的四种方法
- 遍历HashMap的四种方法
- 遍历HashMap的四种方法
- 遍历HashMap的四种方法
- HashMap的四种遍历
- hashMap的四种遍历
- 遍历HashMap的四种
- HashMap的四种遍历方法,及效率比较
- HashMap的四种循环遍历方式
- 遍历HashMap的四种方式
- HashMap的四种遍历方式
- Java -- HashMap的四种遍历方式
- 遍历HashMap的四种方式
- 遍历Hashmap的四种方式
- Marmoset Toolbag 3烘焙使用参数详解
- System.Threading.Tasks.Task.ThrowIfExceptional
- 程序员1
- UMDFaces数据集与人脸特征点检测
- 设计模式C++实现(5)——原型模式、模板方法模式
- 遍历HashMap的四种方法
- [LeetCode]Fraction to Recurring Decimal
- 如何解决 定位和float 造成的脱离文档流
- [FUNC]exchangeSort
- Nginx配置HTTPS和HTTP
- 要登录到这台远程计算机 必须被授予允许通过终端服务登录的权限
- (POJ 1703) Find them, Catch them【典型并查集:判断在不同的集合】
- ubuntu16.04主题美化和软件推荐
- 现场支援-问题解决记录