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

}

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