1、增
- put(key, value); 存放的是key-value对
public static void main(String[] args) {
Map<String, Student> map = new HashMap<>();
map.put("xiaobai", new Student("xiaobai", 10));
map.put("xiaohei", new Student("xiaobai", 10));
System.out.println(map.size());
System.out.println(map.get("xiaobai"));
System.out.println(map.get("xiaohei"));
}
执行结果:
1
xiaobai 10
xiaobai 10
这里可以看到,map中可以存放相同的value,但是不能放相同的key。
2、删
- map.remove(key); 这个方法返回值是要删除的key对应的value。
- map.remove(key, value); 这个方法的返回值是true或false,说明看是否删除成功。
public static void main(String[] args) {
Map<String, Student> map = new HashMap<>();
map.put("xiaobai", new Student("xiaobai", 10));
map.put("xiaohei", new Student("xiaohei", 20));
System.out.println(map);
//根据key值删除
System.out.println(map.remove("xiaobai"));
System.out.println(map);
}
执行结果:
{xiaobai=xiaobai 10, xiaohei=xiaohei 20}
xiaobai 10
{xiaohei=xiaohei 20}
public static void main(String[] args) {
Map<String, Student> map = new HashMap<>();
Student s0 = new Student("xiaobai", 10);
Student s1 = new Student("xiaohei", 20);
map.put("xiaobai", s0);
map.put("xiaohei", s1);
System.out.println(map);
//根据key和value值删除
System.out.println(map.remove("xiaobai", s0));
System.out.println(map);
}
执行结果:
{xiaobai=xiaobai 10, xiaohei=xiaohei 20}
true
{xiaohei=xiaohei 20}
3、改
-
put(key, value); 这里大家应该比较疑惑,put(key, value);不是增加元素的方法吗?但是在这里,我们由于设置了相同的key值,不同的value值,所以我们后一个插入的key-value将前一个key-value给顶替掉了。前一个key-value就不生效,然后就等着被回收了。
-
replace(key, value); 替换和上面的那个方法效果一致。
public static void main(String[] args) {
Map<String, Student> map = new HashMap<>();
map.put("xiaobai", new Student("xiaobai", 10));
map.put("xiaobai", new Student("xiaohei", 20));
System.out.println(map.get("xiaobai"));
}
执行结果:
xiaohei 20
4、查
- get(key); 这里拿到key值,就可以取出对应的value值。(get()方法的参数不能传int型的参数,因为下标是用key的hash值算出来的,所以你根本不知道具体元素真正的角标)。
- map.containsKey(key); 查看当前容器里面是否有对应的key值
- map.containsValue(value) 查看当前容器里面是否有对应的value值
public static void main(String[] args) {
Map<String, Student> map = new HashMap<>();
map.put("xiaobai", new Student("xiaobai", 10));
map.put("xiaohei", new Student("xiaohei", 20));
System.out.println(map.get("xiaohei"));
}
执行结果:
xiaohei 20
public static void main(String[] args) {
Map<String, Student> map = new HashMap<>();
Student s0 = new Student("xiaobai", 10);
Student s1 = new Student("xiaohei", 20);
map.put("xiaobai", s0);
map.put("xiaohei", s1);
//是否包含key值
System.out.println(map.containsKey("xiaobai"));
//是否包含value值
System.out.println(map.containsValue(s0));
}
执行结果:
true
true
5、遍历
由于map不能用迭代器进行遍历,所以只能用以下两种方式遍历。
①纵切遍历:先拿出map中所有的key值,然后根据key值拿出对应的value值。
- map.keySet(); 将map中所有的key值,全部存到set中去,然后遍历set集合拿出对应的value。
public static void main(String[] args) {
Map<String, Student> map = new HashMap<>();
Student s0 = new Student("xiaobai", 10);
Student s1 = new Student("xiaohei", 20);
Student s2 = new Student("xiaolan", 30);
map.put("xiaobai", s0);
map.put("xiaohei", s1);
map.put("xiaolan", s2);
Set<String> keys = map.keySet();
for (String s : keys) {
System.out.println(s + " : " + map.get(s));
}
}
执行结果:
xiaobai : xiaobai 10
xiaolan : xiaolan 30
xiaohei : xiaohei 20
②横切遍历:拿到对应的Entry,然后就可以遍历map集合了。
public static void main(String[] args) {
Map<String, Student> map = new HashMap<>();
Student s0 = new Student("xiaobai", 10);
Student s1 = new Student("xiaohei", 20);
Student s2 = new Student("xiaolan", 30);
map.put("xiaobai", s0);
map.put("xiaohei", s1);
map.put("xiaolan", s2);
Set<Map.Entry<String, Student>> set = map.entrySet();
for (Map.Entry<String, Student> entry : set) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
执行结果:
xiaobai : xiaobai 10
xiaolan : xiaolan 30
xiaohei : xiaohei 20