package com.lovecoding.day19.map01;

import org.junit.Test;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class TestMap01 {
    // 4、元视图操作的方法：

    //* Set<K> keySet()
    //* Collection<V> values()
    //* Set<Map.Entry<K,V>> entrySet()
    @Test
    public void test04() {
        Map<Integer, String> map = new HashMap<>();
        //key 不重复   value 可以重复
        map.put(1, "张三");
        map.put(2, "李四");
        map.put(3, "王五");

        //遍历
        Set<Integer> integers = map.keySet();

        for (Integer integer : integers) {
            String v = map.get(integer);
            System.out.println("k:"+integer+"-v："+v);
        }

        //values
        Collection<String> values = map.values();
        for (String value : values) {
            System.out.println(value);
        }

        // entrySet()
        for (Map.Entry<Integer, String> entry : map.entrySet()) {
            Integer key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"-"+value);
        }


        System.out.println(map.size());

    }



    // 3、元素查询的操作
    //* V get(Object key)
    //* boolean containsKey(Object key)
    //* boolean containsValue(Object value)
    //* boolean isEmpty()
    @Test
    public void test03() {

        Map<Integer, String> map = new HashMap<>();
        //key 不重复   value 可以重复
        map.put(1, "张三");
        map.put(2, "李四");
        map.put(3, "王五");

        String s = map.get(1);
        System.out.println(s);

        String s1 = map.get(2);
        System.out.println(s1);
        //删除
        map.clear();
        boolean b = map.containsKey(1);
        System.out.println(b);

        boolean ww = map.containsValue("王五");
        System.out.println(ww);

        System.out.println(map.isEmpty());
    }


    //2、删除
    //* void clear()
    //* V remove(Object key)
    @Test
    public void test02() {

        Map<Integer, String> map = new HashMap<>();
        //key 不重复   value 可以重复
        map.put(1, "张三");
        map.put(2, "张三");
        map.put(3, "张三");


        String remove = map.remove(1);
        System.out.println(remove);

        map.clear();
        System.out.println(map);
    }

    //1、添加操作
    //* V put(K key,V value)
    //* void putAll(Map<? extends K,? extends V> m)
    @Test
    public void test01(){

        Map<Integer,String> map = new HashMap<>();
        //key 不重复   value 可以重复
        map.put(1,"张三");
        map.put(2,"张三");
        map.put(3,"张三");

        Map<Integer,String> map1 = new HashMap<>();
        //key 不重复   value 可以重复
        map1.put(1,"李四");
        map1.put(2,"李四");
        map1.put(4,"李四");

        //添加 相同key 不重复 ，value 替换 之后的值会替换之前的值
        map.putAll(map1);

        System.out.println(map);
    }


}
