package com.day12.json;

import org.junit.Test;

import java.util.*;

/**
 * Author: Json
 * Date: 2021/9/27
 **/
public class JsonTest {
    public static void main(String[] args) {
        System.out.println("map集合的使用");
        // map
        //  |--  HashMap   主要实现类  线程不安全的 效率高 ; 存储null的key和value
        //     |--   LinkedHashMap  保证遍历map元素时,可以按照添加的顺序实现遍历
        //                原因:在原有的HashMap底层结构基础上 添加了一对指针,指向前一个和后一个元素
        //                对于频繁的遍历操作 此类执行效率高于HashMap
        //  |--  TreeMap     保证按照添加的key-value 对进行排序  实现排序遍历,此时考虑key的自然排序或定制排序
        //             底层使用的红黑树
        //  |--  Hashtable   作为古老的实现类  线程安全 效率低
        //     |--    Properties: 常用来处理配置文件 key和value都是String类型
    }

//面试题
    //  HashMap的底层的实现原理?
    //在实例化以后 底层创建了长度为16的一维数组Entry[] table
    // map.put(key,value);

    // HashMap和Hashtable的异同?
    // CurrentHashMap(线程安全) 和Hashtable的异同?
    @Test
    public void test1(){
        Map map=new HashMap();
        map.put(null,"json");
        System.out.println(map);

        //map 常用方法
        //添加
        map.put(1231,"json");
        System.out.println(map);

        //修改
        map.put(1231,"wl");
        System.out.println(map);
        Map map1=new HashMap();

        //添加多个 putAll
        map1.put("123","31232");
        map.putAll(map1);
        System.out.println(map);

        //移除
        map.remove("123");
        System.out.println(map);


        //清空里的数据
//        map.clear();  //
//        System.out.println(map.size());

        //获取map某值 get
        System.out.println(map.get(1231));

        //判断是否key存在
        boolean isKey=map.containsKey("213");
        System.out.println(isKey);

        //判断是否包含value
        boolean isValue=map.containsValue("2131");
        System.out.println(isValue);

        //判断map是否为空
        System.out.println(map.isEmpty());

        //判断当前map和参数对象obj 是否相等
        System.out.println(map.equals(new Object()));

        //如果遍历map的所有key
        // map中的key :无需的 不可重复的  使用set存储所有的key   key所在类中需要重写equals()和hashCode()
        Set set=map.keySet();
        Iterator iterator=set.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }

        //map中的value : 无序的 可重复的 使用Collection存储所有的value  value所在的类中要重写equals()
        //遍历所有的value
        Collection collection=map.values();
        for (Object obj :collection){
            System.out.println(obj);
        }

        //遍历所有的key-value
        //方式一 entrySet();
        Set set1=map.entrySet();
        Iterator iterator1= set1.iterator();
        while (iterator1.hasNext()){
            Object obj=iterator1.next();
            //entrySet 集合中元素都是entry
            Map.Entry entry=(Map.Entry) obj;
            System.out.println(entry.getKey()+"===>"+entry.getValue());
        }

        //方式二
        Set set2=map.keySet();
        Iterator iterator2=set2.iterator();
        while (iterator2.hasNext()){

            System.out.println(iterator2.next()+"==>"+map.get(iterator2.next()));
        }

    }
}