第一种:
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 ) ...{
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 ) ...{
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中,所以就快了。
到底keyset快还是entryset快,这个例子输出太多影响效果,改一下例子。
- public class HashMapTest {
- private static int MAXKEY = 1234567;
- public static void main(String[] args) {
- HashMap hashmap = new HashMap();
- for (int i = 0; i < HashMapTest.MAXKEY; i++) {
- hashmap.put(i, "thanks");
- }
- long bs = Calendar.getInstance().getTimeInMillis();
- Iterator iterator = hashmap.keySet().iterator();
- while (iterator.hasNext()) {
- Object key = iterator.next();
- Object value = hashmap.get(key);
- }
- System.out.print(Calendar.getInstance().getTimeInMillis() - bs + ",");
- listHashMap();
- }
- public static void listHashMap() {
- HashMap hashmap = new java.util.HashMap();
- for (int i = 0; i < HashMapTest.MAXKEY; i++) {
- hashmap.put(i, "thanks");
- }
- long bs = Calendar.getInstance().getTimeInMillis();
- Iterator it = hashmap.entrySet().iterator();
- Map.Entry entry;
- while (it.hasNext()) {
- entry = (java.util.Map.Entry) it.next();
- Object key = entry.getKey();
- Object value = entry.getValue();
- //Object value = hashmap.get(key);
- }
- System.out.print(Calendar.getInstance().getTimeInMillis() - bs);
- }
- }
public class HashMapTest {
private static int MAXKEY = 1234567;
public static void main(String[] args) {
HashMap hashmap = new HashMap();
for (int i = 0; i < HashMapTest.MAXKEY; i++) {
hashmap.put(i, "thanks");
}
long bs = Calendar.getInstance().getTimeInMillis();
Iterator iterator = hashmap.keySet().iterator();
while (iterator.hasNext()) {
Object key = iterator.next();
Object value = hashmap.get(key);
}
System.out.print(Calendar.getInstance().getTimeInMillis() - bs + ",");
listHashMap();
}
public static void listHashMap() {
HashMap hashmap = new java.util.HashMap();
for (int i = 0; i < HashMapTest.MAXKEY; i++) {
hashmap.put(i, "thanks");
}
long bs = Calendar.getInstance().getTimeInMillis();
Iterator it = hashmap.entrySet().iterator();
Map.Entry entry;
while (it.hasNext()) {
entry = (java.util.Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
//Object value = hashmap.get(key);
}
System.out.print(Calendar.getInstance().getTimeInMillis() - bs);
}
}
/*
* 先来测测,i为1234567时,输出 63,47 47,47 63,47 测了三次
* 改一下代码,listHashMap方法的while循环中,Object value = hashmap.get(key)
* 再测,63,79 47,78 63,79 三次
*/
keySet比entrySet慢吗?
keySet第一次遍历获得所有key,entrySet第一次遍历获得所有key和value,这一步显然entrySet会慢,看下一步。
iterator之后,keySet的next()是key,entrySet的next()是Map.Entry(包含了key和value)。
要按key排序的话,似乎keySet更方便一些。
- Object[] key_arr = hashmap.keySet().toArray();
- Arrays.sort(key_arr);
- for (Object key : key_arr) {
- Object value = hashmap.get(key);
- }