您现在的位置是:首页 >

数据结构中序遍历 java中怎么遍历HashMap

火烧 2022-07-05 16:21:52 1069
java中怎么遍历Ha hMa   一  Ha hMa taff = ew Ha hMa   添加关键字值对 自己写遍历  Set e trie = taff e trySet   Iterator

java中怎么遍历HashMap  

  

  HashMap staff = new HashMap();

  添加关键字值对 自己写遍历

  Set entries = staff entrySet();

数据结构中序遍历 java中怎么遍历HashMap

  Iterator iter = entries iterator();

  while(iter hasNext())

  {

  Map Entry entry = (Map Entry)iter next();

  Object key = entry getKey();得么关键字

  Object value = entry getValue();得到值

  }

  

  Map map = new HashMap();

  for (Iterator iter = map entrySet() iterator(); iter hasNext();) {

  Map Entry entry = (Map Entry) iter next(); //map entry 同时取出键值对

  Object key = entry getKey();

  Object val = entry getValue();

  }

  

  Map map = new HashMap();

  for (Iterator iter = map keySet() iterator(); iter hasNext();) {

  Object key = iter next();

  Object val = map get(key);

  }

  Iterator是迭代器

  对于keySet其实是遍历了 次 一次是转为iterator 一次就从hashmap中取出key所对于的value

  而entryset只是遍历了第一次 他把key和value都放到了entry中 所以就快了

  对于我们做web的 可能不部分都是用vo对象或是form封装信息 所以用到hashmap时 其内存放的都是上面的对象 因此使用entryset遍历性能会有所提高

lishixinzhi/Article/program/Java/JSP/201311/19815  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码