- package it;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- public class A
- {
-
- public static void main(String[] args)
- {
- Map<String, Integer> map = new HashMap<String,Integer>();
- map.put("zhangsan", 18);
- map.put("lisi", 21);
- map.put("wangwu", 19);
- map.put("zhouliu", 46);
- // System.out.println(map.size());
- /*//通过keySet取出
- Set<String> keySet = map.keySet();
- Iterator<String> it = keySet.iterator();
- while (it.hasNext())
- {
- String name = it.next();
- int age = map.get(name);
- //System.out.println(name+":::"+age);
-
-
- }*/
-
-
- //通过entrySet取出
- Set<Map.Entry<String, Integer>>entrySet = map.entrySet();
- Iterator<Map.Entry<String,Integer>> iterator = entrySet.iterator();
- while (iterator.hasNext())
- {
- Map.Entry<String, Integer> entry= iterator.next();
- String name=entry.getKey();
- int age = entry.getValue();
- System.out.println(name+":::"+age);
- if (name.equals("zhangsan"))
- {
-
- entry.setValue(88);
- }
- System.out.println(name+":::"+age);
- }
- }
- }
复制代码 |