javaHashMap遍历

  java Map 遍历速度最优解

  第一种:

  Map map = new HashMap();Iterator iter = map.entrySet(erator();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(erator();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(erator();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(erator();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.LinkedHashMapimport 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 stubHashMap 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(erator();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=erator(); 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=erator(); 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=erator(); 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———————-hellothanksokbye——————–遍历HashMap的value———————-你好谢谢好的再见

接受自己的失败面,是一种成熟,更是一种睿智

javaHashMap遍历

相关文章:

你感兴趣的文章:

标签云: