首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

取HashMap聚合中的key和value的三种方法

2012-11-06 
取HashMap集合中的key和value的三种方法?public static? void main(String[] args){??Map mapnew HashMap

取HashMap集合中的key和value的三种方法

?public static? void main(String[] args){
??Map map=new HashMap();
??map.put("1", 21);
??map.put("2", 123);
??map.put("3", 98);
??
//?? 方法1??? 此方法效率比较高?
??Iterator ite=map.entrySet().iterator();
??while(ite.hasNext()){
???Entry string=(Entry)ite.next();
??????? System.out.print(string.getKey()+"/");?
??????? System.out.println(string.getValue());?
??}

?

//?? 方法2 此方法效率比较低??
???? Iterator iteKey=map.keySet().iterator();
???? while(iteKey.hasNext()){
???? ?Object key=iteKey.next();
??????????? Object value=map.get(key);
??????????? System.out.print(key+"/");
??????????? System.out.println(value);
???? ?
???? }
??
//? 方法3?
??? Set<Entry<String, Integer>> set = map.entrySet();
??????? for(Entry<String, Integer> entry: set)
??????? {
??????????? System.out.println(entry.getKey() + "/" + entry.getValue());
??????? }
?}

热点排行