Java中Map的简单用法

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 :

遍历key:

Map map = new HashMap(); 
for (Integer key : map.keySet()) { 
  System.out.println("Key = " + key); 
} 

遍历value:

for (Integer value : map.values()) { 
  System.out.println("Value = " + value); 
}

遍历key和value:

Map map = new HashMap(); 
for (Map.Entry entry : map.entrySet()) { 
  System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); 
}

你可能感兴趣的:(java,笔记)