java Map 遍歷速度最優解java
第一種:element
Map map = new HashMap();get
Iterator iter = map.entrySet().iterator();hash
while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next();it
Object key = entry.getKey();io
Object val = entry.getValue();table
}class
效率高,之後必定要使用此種方式!效率
第二種:import
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中,因此就快了。 注:Hashtable的遍歷方法和以上的差很少!
進行實例分析一下下:
如下經過程序來簡單實踐一下HashMap的的遍歷 若是要保持HashMap的遍歷順序和原插入順序一致,可使用LinkedHashMap,使用方法和HashMap同樣,改一下聲明便可:LinkedHashMap myMap = new LinkedHashMap();
固然須要導入:
java.util.LinkedHashMap import java.util.Collection;
import java.util.HashMap; import java.util.Iterator;
import java.util.Map; public class MapList {
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap myMap = new HashMap();
myMap.put("hello", "你好");
myMap.put("bye", "再見");
myMap.put("thanks", "謝謝");
myMap.put("ok", "好的");
System.out.println("--------------------遍歷key和value----------------------");
for(Iterator iter = myMap.entrySet().iterator();
iter.hasNext();)
{
Map.Entry element = (Map.Entry)iter.next();
Object strKey = element.getKey();
Object strObj = element.getValue();
System.out.println("myMap.get(\""+strKey+"\")="+strObj); }
System.out.println();
System.out.println("--------------------遍歷整個HashMap----------------------");
Collection objs = myMap.entrySet();
for (Iterator iterator=objs.iterator();
iterator.hasNext();){
Object obj = iterator.next();
System.out.println(obj);
}
System.out.println();
System.out.println("--------------------遍歷HashMap的key----------------------");
Collection keys = myMap.keySet();
for (Iterator iterator=keys.iterator();
iterator.hasNext();
)
{
Object key = iterator.next();
System.out.println(key);
}
System.out.println();
System.out.println("--------------------遍歷HashMap的value----------------------");
Collection values = myMap.values(); for (Iterator iterator=values.iterator(); iterator.hasNext();
)
{
Object value = iterator.next();
System.out.println(value);
}
}
}
運行結果: --------------------遍歷key和value---------------------- myMap.get("hello")=你好
myMap.get("thanks")=謝謝 myMap.get("ok")=好的 myMap.get("bye")
=再見
--------------------遍歷整個HashMap---------------------- hello=你好 thanks=謝謝 ok=好的 bye=再見 --------------------遍歷HashMap的key---------------------- hello thanks ok bye --------------------遍歷HashMap的value----------------------
你好
謝謝
好的
再見