熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

如何遍歷一個HashMap

2013-11-23 19:34:33  來源: Java核心技術 

  以下是兩種方法遍歷HashMap的代碼示例

  方法

  Map map = new HashMap();  mapput(a );  mapput(b );  mapput(c );  mapput(d );  mapput(e );  for (Iterator iter = mapkeySet(erator(); iterhasNext();) {      Object key = iternext();      Object val = mapget(key);      Systemoutprintln(key:+key);      Systemoutprintln(value:+val);  }

  方法

  view sourceprint?Map map = new HashMap();

  mapput(a );

  mapput(b );

  mapput(c );

  mapput(d );

  mapput(e );

  for (Iterator iter = mapentrySet(erator(); iterhasNext();) {

  MapEntry entry = (MapEntry) iternext();

  Object key = entrygetKey();

  Object val = entrygetValue();

  Systemoutprintln(key:+key);

  Systemoutprintln(value:+val);

  }

  view sourceprint?<%

  Map<String Integer> map = (Map<String Integer>) sessiongetAttribute(UserInfo);

  outprintln(map == null);

  if (map == null)

  {

  map = new HashMap<String Integer>();

  outprintln(map == null);

  mapput(steden );

  mapput(steden );

  mapput(steden );

  mapput(steden );

  }

  sessionsetAttribute(UserInfo map);

  for(MapEntry<String Integer> item : mapentrySet())

  {

  outprintln(itemgetKey());

  outprintln(itemgetValue());

  outprintln(<br />);

  }

  %>

  性能從低到高


From:http://tw.wingwit.com/Article/program/Java/hx/201311/27112.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.