Map 取值简介方便

 Map map=new HashMap();

map.put(1,"ceshi");

map.put(2,"ceshiu01");

Iterator it=map.keyset().iterator();

 int i=0;

while(it.Hashnext){

  i=it.next();

  logger.info("通过键取对应的值"+map.get(i));

}

之前对Map 取值都通过先取出来的keySet(),先取出来键。然后通过键取对应的值在while中。实在是太麻烦了。

通过一个简洁的取值方式 Entry 接口。直接取就行了 想取多少,取多少。 废话不多说,直接上代码

 Map map=new HashMap();

map.put(1,"ceshi");

map.put(2,"ceshiu01");

Iterator it=map.entryset().iterator();

 int i=0;

while(it.Hashnext){

   Map.Entry<int,String) me=(Map.Entry<int,String))it.next();

  logger.info("键"+me.getkey());

logger.info("键对应的值"+m.getvalue());

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章