map遍历

在Java代码中HashMap的遍历有两种常用的方法,那就是使用keyset及entryset来进行遍历,但两者的遍历速度是有差别的。

       第一种: 使用键-值对映射项。

  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();

  }

  效率高,以后一定要使用此种方式!

  第二种:使用键的Set视图。

  Map map = new HashMap();

  Iterator iter = map.keySet().iterator();

  while (iter.hasNext()) {

           Object key = iter.next();

           Object val = map.get(key);

  }

  效率低,以后尽量少使用!

  代码实例:

      

public class HashMapTest {
         public static void main(String[] args) {
                   HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
                   for (int i = 0; i < 500000; i++) {
                            hashmap.put(i, "thanks");
                   }
                   long bs = Calendar.getInstance().getTimeInMillis();
                   Iterator<Integer> iterator = hashmap.keySet().iterator();
                   while (iterator.hasNext()) {
                            System.out.print(hashmap.get(iterator.next()));
                   }
                   System.out.println(Calendar.getInstance().getTimeInMillis() - bs);
         }
         public static void main2(String[] args) {
                   HashMap<Integer, String> hashmap = new java.util.HashMap<Integer, String>();
                   for (int i = 0; i < 500000; i++) {
                            hashmap.put(i, "thanks");
                   }
                   long bs = Calendar.getInstance().getTimeInMillis();//9297
                   Iterator it = hashmap.entrySet().iterator();
                   while (it.hasNext()) {
                            Map.Entry<Integer, String> entry = (Map.Entry<Integer, String> ) it.next();
                            // entry.getKey() 返回与此项对应的键
                            // entry.getValue() 返回与此项对应的值
                            System.out.print(entry.getValue());
                   }
                   System.out.println(Calendar.getInstance().getTimeInMillis() - bs);
         }
}

  

  对于keySet其实是遍历了2次,一次是转为iterator,一次就从hashmap中取出key所对于的value。而entryset只是遍历了第一次,他把key和value都放到了entry中,所以就快了

HashMap roleHm = (HashMap)this.getRequest().getSession().getAttribute("roleHm");
  boolean flag = (Boolean)this.getRequest().getSession().getAttribute("isKy");
  //如果session 中的 roleHm 为空,则重新读取;
  if(Utils.isNullOrEmpty(roleHm)){
  
   IRoleManager rm = ManagerFactory.getRoleManager();
   AppRole[] roles = rm.getUserRoles(Integer.parseInt(UIMConfig
     .getInstance().appID), userID);
   roleHm = new HashMap();
   // add by  科研人员按需求是不能看到高级查询的 故科研人员的角色是 10191
   long targetRoleId = 10191;
   for (int i = 0; i < roles.length; i++) {
    roleHm.put(Long.toString(roles[i].getId()), roles[i]);
    if (null != roles[i] && roles[i].getId() == targetRoleId) {
     flag = true;
    }
   }
  }
   /*  
   java.util.Iterator iterator = (java.util.Iterator)roleHm.keySet().iterator();
   while(iterator.hasNext()){
    String value = (String)roleHm.get(iterator.next());
   }
   */
 

你可能感兴趣的:(map遍历)