2021-05-8set接口和map接口

list

HashSetDemo

/**
 * @qvthor liuwenzheng
 * @date 2021/5/8 9:16
 */
public class HashSetDemo {
    public static void main(String[] args) {
        HashSetset = new HashSet<>();  // <> 钻石语法
        set.add("111");
        set.add("xxx");
        set.add("上显");
        set.add("333");
        System.out.println(set);
        set.remove("xxx"); // 根据内容移除
        System.out.println(set);
        int size = set.size();
        boolean  empty = set.isEmpty(); //判断是否为空
        System.out.println(size);
        System.out.println(empty);
        set.clear(); //清楚
        System.out.println(set);

    }
}

TreeSetDemo

/**
 * @qvthor liuwenzheng
 * @date 2021/5/8 9:26
 */
public class TreeSetDemo {
    public static void main(String[] args) {
        TreeSet set = new TreeSet<>();
        set.add("111");
        set.add("222");
        set.add("地理");
        set.add("化学");
        set.add("abc");
        set.add("efg");
        System.out.println(set);
    }
}

HashMapDemo

/**
 * @qvthor liuwenzheng
 * @date 2021/5/8 10:06
 */
public class HashMapDemo {
    public static void main(String[] args) {
        HashMap map = new HashMap<>();  //DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 默认的初始值为16
        map.put("henan","河南");  //entry  实例
        map.put("hebei","河北");
        map.put("hubei", "湖北");
        map.put("hubei", "湖北1");  //键重复会覆盖掉原有的值


        map.put("null","空1");
        map.put(null,"空2");
        System.out.println(map);

        for (Map.Entry m : map.entrySet()){
            System.out.println(m);
        }
        for (String  k  : map.keySet()){
            System.out.println(k + "=" + map.get(k));

        }
        for (String v : map.values()){
            System.out.println(v);
        }
    }
}
        //DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 默认的初始值为16
        //DEFAULT_LOAD_FACTOR = 0.75f; 当体内容量达到全部的百分之75的时候直接扩容
        //TREEIFY_THRESHOLD = 8;  当长度为8的时候树化
        //UNTREEIFY_THRESHOLD = 6; 当长度为6的时候 退化为数组
        //MAXIMUM_CAPACITY = 1 << 30;  默认的最大  = 2的30次方

TrueMapDemo

/**
 * @qvthor liuwenzheng
 * @date 2021/5/8 11:03
 */
public class TrueMapDemo {
    public static  void main(String[] args) {
        TreeMap map = new TreeMap<>();  //DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 默认的初始值为16
        map.put("henan","河南");  //entry  实例
        map.put("hebei","河北");
        map.put("hubei", "湖北");
        map.put("hubei", "湖北1");  //键重复会覆盖掉原有的值


        map.put("null","空1");
        map.put(null,"空2");
        System.out.println(map);

        for (Map.Entry m : map.entrySet()){
            System.out.println(m);
        }
        for (String  k  : map.keySet()){
            System.out.println(k + "=" + map.get(k));

        }
        for (String v : map.values()){
            System.out.println(v);
        }
    }
}
//DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 默认的初始值为16
//DEFAULT_LOAD_FACTOR = 0.75f; 当体内容量达到全部的百分之75的时候直接扩容
//TREEIFY_THRESHOLD = 8;  当长度为8的时候树化
//UNTREEIFY_THRESHOLD = 6; 当长度为6的时候 退化为数组
//MAXIMUM_CAPACITY = 1 << 30;  默认的最大  = 2的30次方

你可能感兴趣的:(2021-05-8set接口和map接口)