import java.util.*;
/**
* java map集合是key=>value的元素集合
*/
public class MapTest {
public static void main(String[] args){
Map<String, Integer> m1 = new HashMap<String, Integer>();
Map<String, String> m2 = new HashMap<String, String>();
m2.put("xiaogang", "男");
m2.put("xiaoli", "女");
m1.put("wang", 1);
m1.put("li", 2);
m1.put("zhang", 3);
System.out.println(m1.get("zhang"));
m1.put("zhang", 5); //出现重复key,会更新value
System.out.println(m1.get("wang"));
System.out.println(m2.get("xiaoli"));
System.out.println(m1.keySet()); //将所有key转为set集合
System.out.println(m1.values()); //将所有value转为list
}
}
运行结果:
3
1
女
[li, zhang, wang]
[2, 5, 1]