File size: 2,056 Bytes
0284885
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package Map_package;

import java.util.HashMap;
import java.util.Map;

public class Map_demo1 {
    public static void main(String[] args) {
        /*
        V put (K key, V value)                  添加元素
        V remove (Object key)                   根据键删除键值对元素
        void clear()                            移除所有的键值对元素
        boolean containsKey (Object obj)        判断集合是否包含指定的键
        boolean containsValue (Object value)    判断集合是否包含指定的值
        boolean isEmpty ()                      判断集合是否为空
        int size ()                             集合的长度,也就是集合中键值对的个数
         */

        //1、创建Map集合的对象
        Map<String, String> m = new HashMap<>();

        //2、添加元素
        //put方法的细节:
        //添加/覆盖
        //在添加数据的时候,如果键不存在,那么直接把键值对对象添加到map集合当中
        //在添加数据的时候,如果键是存在的,那么会把原有的键值对对象覆盖,会把被覆盖的值进行返回。
        String value1 = m.put("郭靖", "黄蓉");
        System.out.println(value1);

        m.put("韦小宝", "沐剑屏");
        m.put("尹志平", "小龙女");

        String value2 = m.put("韦小宝", "双儿");
        System.out.println(value2);

        //3、删除
        String value3 = m.remove("郭靖");
        System.out.println(value3);

        //4、清空
        //m.clear();

        //5、判断是否包含
        boolean result1 = m.containsKey("郭靖");
        System.out.println(result1);//false

        boolean result2 = m.containsValue("小龙女");
        System.out.println(result2);//true

        //6、判断是否为空
        boolean result3 = m.isEmpty();
        System.out.println(result3);

        //7、长度
        int size = m.size();
        System.out.println(size);


        //3、打印集合
        System.out.println(m);
    }
}