Map集合类详解(附加思维导图)

目录

一、Map集合类思维导图

 二、Map集合类常用方法

2.1、HashMap示意图

 2.2、HashMap的常用方法

 2.3、TreeMap集合常用方法


一、Map集合类思维导图

Map集合类详解(附加思维导图)_第1张图片

 二、Map集合类常用方法

2.1、HashMap示意图

Map集合类详解(附加思维导图)_第2张图片

 2.2、HashMap的常用方法

①.put(K key, V value) 将键(key)/值(value)映射存放到Map集合中

public class Test {
    public static void main(String[] args) {
        HashMap map = new HashMap();
        map.put("Tom", 100);//向HashMap中添加元素  
    }
}

②.get(Object key) 返回指定键所映射的值,没有该key对应的值则返回 null,即获取key对应的value。

public class Test {
    public static void main(String[] args) {
        HashMap map = new HashMap();
        map.put("Tom", 100);
        map.put("Tom", 0);
        int score = map.get("Tom");// 获取key对应的value
        System.out.println(score);// key不允许重复,若重复,则覆盖已有key的value
    }
}

Map集合类详解(附加思维导图)_第3张图片

可知,之前加入的value已被覆盖,前面的观点得证 

③. size()  返回Map集合中数据数量,准确说是返回key-value的组数。

public class Test {
    public static void main(String[] args) {
        HashMap map = new HashMap();
        map.put("Tom", 100);
        map.put("Jim", 90);
        map.put("Sam", 91);
        System.out.println(map.size());
    }
}

④:clear() 清空Map集合

public class Test {
    public static void main(String[] args) {
        HashMap map = new HashMap();
        map.put("Tom", 100);
        map.put("Jim", 90);
        map.put("Sam", 91);
        map.clear();// 清空map中的key-value
        System.out.println(map.size());
    }
}

 ⑤:isEmpty () 判断Map集合中是否有数据,如果没有则返回true,否则返回false

public class Test {
    public static void main(String[] args) {
        HashMap map = new HashMap();
        map.put("Tom", 100);
        map.put("Jim", 90);
        map.put("Sam", 91);
        map.clear();// 清空map中的key-value
        System.out.println(map.isEmpty());
    }
}

⑥:remove(Object key) 删除Map集合中键为key的数据并返回其所对应value值。

public class Test {
    public static void main(String[] args) {
        HashMap map = new HashMap();
        map.put("Tom", 100);
        map.put("Jim", 90);
        map.put("Sam", 91);
        map.remove("Tom");
        System.out.println(map);
    }
}

⑦:containsKey(Object key) Hashmap判断是否含有key

public class Test {
    public static void main(String[] args) {
        HashMap map=new HashMap<>();
         /*boolean*///判断map中是否存在这个key
         System.out.println(map.containsKey("DEMO"));//false
         map.put("DEMO", 1);
         System.out.println(map.containsKey("DEMO"));//true
    }
}

⑧:containsValue(Object value) Hashmap判断是否含有value:

public class Test {
    public static void main(String[] args) {
        HashMap map=new HashMap<>();
        /*boolean*///判断map中是否存在这个value
        System.out.println(map.containsValue(1));//false
        map.put("DEMO", 1);
        System.out.println(map.containsValue(1));//true
    }
}

⑨:Hashmap添加另一个同一类型的map下的所有数据

public class Test {
    public static void main(String[] args) {
        HashMap map=new HashMap<>();
        HashMap map1=new HashMap<>();
        /*void*///将同一类型的map添加到另一个map中
        map1.put("DEMO1", 1);
        map.put("DEMO2", 2);
        System.out.println(map);//{DEMO2=2}
        map.putAll(map1);
        System.out.println(map);//{DEMO1=1, DEMO2=2}
    }
}

⑩:Hashmap替换这个key的value

public class Test {
    public static void main(String[] args) {
        HashMap map=new HashMap<>();
        /*value*///判断map中是否存在这个key
        map.put("DEMO1", 1);
        map.put("DEMO2", 2);
        System.out.println(map);//{DEMO1=1, DEMO2=2}
        System.out.println(map.replace("DEMO2", 1));//2
        System.out.println(map);//{DEMO1=1, DEMO2=1}
    }
}

⑪Map.getOrDefault(key,默认值);

Map中会存储——对应的key和value。如果在Map中存在key,则返回key所对应的的value。如果在Map中不存在key,则返回默认值。

Map集合类详解(附加思维导图)_第4张图片

 2.3、TreeMap集合常用方法

  1. put(key, value)将指定的键值对添加到 TreeMap 中,如果键已存在,则会更新对应的值。

  2. get(key)返回指定键对应的值,如果键不存在,则返回 null。

  3. remove(key)从 TreeMap 中移除指定键及其对应的值。

  4. containsKey(key)判断 TreeMap 中是否包含指定的键,如果包含返回 true,否则返回 false。

  5. containsValue(value)判断 TreeMap 中是否包含指定的值,如果包含返回 true,否则返回 false。

  6. size()返回 TreeMap 中键值对的个数。

  7. isEmpty()判断 TreeMap 是否为空,如果为空返回 true,否则返回 false。

  8. firstKey()返回 TreeMap 中最小的键。

  9. lastKey()返回 TreeMap 中最大的键。

  10. keySet()返回一个包含 TreeMap 中所有键的 Set 集合。

  11. values()返回一个包含 TreeMap 中所有值的 Collection 集合。

  12. entrySet()返回一个包含 TreeMap 中所有键值对的 Set 集合。

import java.util.TreeMap;

public class TreeMapExample {
    public static void main(String[] args) {
        // 创建一个 TreeMap 对象
        TreeMap treeMap = new TreeMap<>();

        // 添加键值对到 TreeMap 中
        treeMap.put("apple", 10);
        treeMap.put("banana", 5);
        treeMap.put("orange", 8);

        // 获取指定键对应的值
        int appleCount = treeMap.get("apple");
        System.out.println("apple 的数量为:" + appleCount);

        // 判断指定键是否存在
        boolean containsBanana = treeMap.containsKey("banana");
        System.out.println("是否包含 banana:" + containsBanana);

        // 移除指定键及其对应的值
        treeMap.remove("orange");
        System.out.println("移除 orange 后的键值对个数:" + treeMap.size());

        // 遍历 TreeMap 中的键值对
        for (String key : treeMap.keySet()) {
            int value = treeMap.get(key);
            System.out.println("键:" + key + ",值:" + value);
        }
    }
}

输出结果:

apple 的数量为:10
是否包含 banana:true
移除 orange 后的键值对个数:2
键:apple,值:10
键:banana,值:5

你可能感兴趣的:(java,开发语言,数据结构)