- import java.util.HashMap;
- public class MapTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
-
- HashMap<String, String> person = new HashMap<String, String>();
- person.put("1", "张三");
- person.put("2", "李四");
- person.put("3", "王五");
- person.put("4", "赵六");
-
- //遍历Map
- for(String key: person.keySet()){
- System.out.println("key:"+key+"---value"+person.get(key));
- }
- System.out.println("遍历map结束");
- //插入key=5 value=王七
- person.put("5", "王七");
- for(String key: person.keySet()){
- System.out.println("key:"+key+"---value"+person.get(key));
- }
- System.out.println("已经插入新的person");
-
- //删除编号为1的人
- person.remove("1");
- //遍历map进行检查
- for(String key: person.keySet()){
- System.out.println("key:"+key+"---value"+person.get(key));
- }
- System.out.println("删除操作已经结束");
- //修改编号2,value为赵四
- person.put("2", "赵四");
- //遍历检查
- for(String key: person.keySet()){
- System.out.println("key:"+key+"---value"+person.get(key));
- }
- System.out.println("修改操作已经结束");
- }
- }
复制代码 |