HashMap遍历的两种方式
来源:互联网 发布:日志服务器软件 编辑:程序博客网 时间:2024/05/22 14:42
第一种:
Map map = new HashMap();
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
}
效率高,以后一定要使用此种方式!
第二种:
Map map = new HashMap();
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
Object key = iter.next();
Object val = map.get(key);
}
效率低,以后尽量少使用!
例:
HashMap的遍历有两种常用的方法,那就是使用keyset及entryset来进行遍历,但两者的遍历速度是有差别的,下面请看实例:
public class HashMapTest {
public static void main(String[] args) ...{
HashMap hashmap = new HashMap();
for (int i = 0; i < 1000 i br style='font-size:14px;font-style:normal;font-weight:normal;color:rgb(0, 0, 0);' /> hashmap.put("" i, "thanks");
}
long bs = Calendar.getInstance().getTimeInMillis();
Iterator iterator = hashmap.keySet().iterator();
while (iterator.hasNext()) ...{
System.out.print(hashmap.get(iterator.next()));
}
System.out.println();
System.out.println(Calendar.getInstance().getTimeInMillis() - bs);
listHashMap();
}
public static void listHashMap() ...{
java.util.HashMap hashmap = new java.util.HashMap();
for (int i = 0; i < 1000 i br style='font-size:14px;font-style:normal;font-weight:normal;color:rgb(0, 0, 0);' /> hashmap.put("" i, "thanks");
}
long bs = Calendar.getInstance().getTimeInMillis();
java.util.Iterator it = hashmap.entrySet().iterator();
while (it.hasNext()) ...{
java.util.Map.Entry entry = (java.util.Map.Entry) it.next();
// entry.getKey() 返回与此项对应的键
// entry.getValue() 返回与此项对应的值
System.out.print(entry.getValue());
}
System.out.println();
System.out.println(Calendar.getInstance().getTimeInMillis() - bs);
}
}
对于keySet其实是遍历了2次,一次是转为iterator,一次就从hashmap中取出key所对于的value。而entryset只是遍历了第一次,他把key和value都放到了entry中,所以就快了。
注:Hashtable的遍历方法和以上的差不多!
Map map = new HashMap();
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
}
效率高,以后一定要使用此种方式!
第二种:
Map map = new HashMap();
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
Object key = iter.next();
Object val = map.get(key);
}
效率低,以后尽量少使用!
例:
HashMap的遍历有两种常用的方法,那就是使用keyset及entryset来进行遍历,但两者的遍历速度是有差别的,下面请看实例:
public class HashMapTest {
public static void main(String[] args) ...{
HashMap hashmap = new HashMap();
for (int i = 0; i < 1000 i br style='font-size:14px;font-style:normal;font-weight:normal;color:rgb(0, 0, 0);' /> hashmap.put("" i, "thanks");
}
long bs = Calendar.getInstance().getTimeInMillis();
Iterator iterator = hashmap.keySet().iterator();
while (iterator.hasNext()) ...{
System.out.print(hashmap.get(iterator.next()));
}
System.out.println();
System.out.println(Calendar.getInstance().getTimeInMillis() - bs);
listHashMap();
}
public static void listHashMap() ...{
java.util.HashMap hashmap = new java.util.HashMap();
for (int i = 0; i < 1000 i br style='font-size:14px;font-style:normal;font-weight:normal;color:rgb(0, 0, 0);' /> hashmap.put("" i, "thanks");
}
long bs = Calendar.getInstance().getTimeInMillis();
java.util.Iterator it = hashmap.entrySet().iterator();
while (it.hasNext()) ...{
java.util.Map.Entry entry = (java.util.Map.Entry) it.next();
// entry.getKey() 返回与此项对应的键
// entry.getValue() 返回与此项对应的值
System.out.print(entry.getValue());
}
System.out.println();
System.out.println(Calendar.getInstance().getTimeInMillis() - bs);
}
}
对于keySet其实是遍历了2次,一次是转为iterator,一次就从hashmap中取出key所对于的value。而entryset只是遍历了第一次,他把key和value都放到了entry中,所以就快了。
注:Hashtable的遍历方法和以上的差不多!
0 0
- HashMap遍历的两种方式
- 遍历HashMap的常用两种方式
- HashMap遍历的两种方式
- HashMap遍历的两种方式
- HashMap遍历的两种方式
- HashMap遍历的两种方式
- hashmap遍历的两种方式
- HashMap遍历的两种方式
- HashMap遍历的两种方式
- 遍历HashMap的两种方式
- HashMap遍历的两种方式
- HashMap遍历的两种方式
- HashMap遍历的两种方式
- HashMap遍历的两种方式
- HashMap遍历的两种方式
- Java遍历HashMap的两种方式
- HashMap遍历的两种方式
- 遍历HashMap的两种方式
- Thread类中sleep是为什么是静态方法
- 源码编译安装lamp环境
- HashMap java.util.ConcurrentModificationException
- Objective-C学习笔记八:类的定义二
- [Leetcode]Minimum Size Subarray Sum
- HashMap遍历的两种方式
- phpstrom如何配置git
- leetcode Binary Tree Preorder Traversal
- 用正则在eclipse中搜寻出所有中文字符
- SELECT INTO , INSERT INTO SELECT 和 CREATE TABLE AS SELECT 的区别
- 查看网卡流量
- Objective-C学习笔记九:继承一
- 8天玩转并行开发——第六天 异步编程模型
- 使用Java获取IP地址实例