java中map集合的用法

来源:互联网 发布:网络盒子看电影软件 编辑:程序博客网 时间:2024/05/16 10:04

 

http://blog.csdn.net/ycyangcai/archive/2009/06/21/4285997.aspx

 

1.声明一个map: Map map = new HashMap(); 
2.向map中放值,注意:map是key-value的形式存放的.如:

     map.put("sa","dd");

3.从map中取值:String str = map.get("sa").toString();结果是:str = "dd";

4.遍历一个map,从中取得key 和value

JDK1.5

 

view plaincopy to clipboardprint?
  1. Map   m   =   new   HashMap();     
  2.   for   (Object   o   :   map.keySet())   {     
  3.           map.get(o);     
  4.   }  
  5.   
  6.    

 

JDK1.4

 

view plaincopy to clipboardprint?
  1. Map   map   =   new   HashMap()   ;     
  2.       
  3.   Iterator   it   =   map.entrySet().iterator()   ;     
  4.   while   (it.hasNext())     
  5.   {     
  6.   Map.Entry   entry   =   (Map.Entry)   it.next()   ;     
  7.   Object   key   =   entry.getKey()   ;     
  8.   Object   value   =   entry.getValue()   ;     
  9.   }   
  10.   
  11.    

 

原创粉丝点击