java中遍历HashMap的方法
来源:互联网 发布:js正则表达式非负整数 编辑:程序博客网 时间:2024/05/19 13:24
Java中,通常有两种遍历HashMap的方法,如下:
import java.util.*;public class MapTest {static HashMap<String, Integer> hashMap = new HashMap<String, Integer>();public static void main(String [] args) {hashMap.put("one", 1);hashMap.put("two", 2);hashMap.put("three", 3);hashMap.put("four", 4);hashMap.put("five", 5);Iterator iter = hashMap.entrySet().iterator();// the first method to travel the mapwhile (iter.hasNext()) {Map.Entry<String, Integer> entry = (Map.Entry<String, Integer>) iter.next();String key = entry.getKey();Integer value = entry.getValue();System.out.println(key + " " + value);}iter = hashMap.keySet().iterator();// the second method to travel the mapwhile (iter.hasNext()) {String key = (String) iter.next();Integer value = hashMap.get(key);System.out.println(key + " " + value);}} // close main()}
第一种效率要高于第二种,应尽量使用第一种进行遍历。
- java中遍历HashMap的方法
- java中遍历HashMap的方法
- Java中Hashmap的遍历方法
- java中遍历HashMap的方法总结
- java中HashMap遍历方法
- java中HashMap遍历方法
- java中HashMap遍历方法
- Java的HashMap遍历方法
- java 遍历hashMap的方法
- java遍历HashMap的方法
- java中HashMap的遍历
- java中HashMap的遍历
- java中HashMap的遍历
- java中遍历hashmap的两种方法及分析
- Java和JSTL标签中遍历HashMap的方法
- 在Java中,HashMap的几种遍历方法
- java中HashMap遍历方法【集合】
- Java中Hashtable,HashMap,WeakHashMap遍历方法
- COCOS2D-X跨ANDROID&IOS平台开发入门教程
- 浅谈保存一个文件操作内部发生了什么
- 如何对n个数进行排序,要求时间复杂度O(n),空间复杂度O(1)
- ADT-20 新的目录规范自动识别libs为库目录导致debug出现NoClassDefFoundError
- 黑马程序员之C#学习笔记:数组的复制四种方法
- java中遍历HashMap的方法
- 匿名共享内存Ashmem实例--C++
- Ubuntu 10.10 源
- [置顶] Android Bluetooth
- 黑马程序员之C#学习笔记:break和continue的区别const与readonly的区别form与control的区别
- JavaSE第七讲:流程控制语句续 Flow Control Statement Cont
- Foundation框架中的数据类型
- CA certutil -view 查询证书有效日期
- CUDA下四重循环两种并行方式效率分析