专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

Map集合方法API实例演示

Map

概述

Map 是一种键-值对(key-value)集合,Map 集合中的每一个元素都包含一个键对象和一个值对象。

以键值对的方式存储数据将键映射到值的对象。一个映射不能包含重复的键,每个键最多只能映射到一个值。

API方法示例

clear

@Test
public void clear() {
    //clear: 清除map集合所有键值对数据
    Map<Integer, String> map = new HashMap<>();
    map.put(1,"clear method");
    map.clear();
    //打印为空
    System.out.println("map = " + map);
}

compute

@Test
public void compute() {
    //compute: 计算指定键映射的值,如果不存在打印为null
    Map<Integer, String> map = new HashMap<>();
    map.put(1,"compute");
    //存在1这个键值,打印完整数据
    map.compute(1, (k, v) -> (v == null) ? "null" : v.concat("map"));
    System.out.println("map = " + map);
}

computeIfAbsent

@Test
public void computeIfAbsent() {
    //computeIfAbsent: 如果不存在指定的键则输出设置的值
    Map<Integer, String> map = new HashMap<>();
    map.put(1,"computeIfAbsent");
    //没有2这个key,则输出右边设置的值
    System.out.println(map.computeIfAbsent(2, key -> "without this key"));
}

computeIfPresent

@Test
public void computeIfPresent() {
    //computeIfPresent: 如果指定的键存在则设置一个新的值,否则为null
    Map<Integer, String> map = new HashMap<>();
    map.put(1,"computeIfPresent");
    System.out.println(map.computeIfPresent(1, (k,v) -> "new element"));
}

containsKey

@Test
public void containsKey() {
    //containsKey: 判断是否包含指定的key,不包含返回false
    Map<Integer, String> map = new HashMap<>();
    //没有1这个key,打印为false
    System.out.println("map.containsKey(1) = " + map.containsKey(1));
}

containsValue

@Test
public void containsValue() {
    //判断是否包含指定的值,不包含则返回false
    Map<Integer, String> map = new HashMap<>();
    map.put(1,"not data");
    //不存在这个指定的值,返回为false
    System.out.println(map.containsValue("containsValues"));
}

entrySet

@Test
public void entrySet() {
    //entrySet: 返回集合的所有数据
    Map<Integer, String> map = new HashMap<>();
    map.put(1,"entrySet");
    Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
    //使用增强for循环遍历key和value
    for (Map.Entry<Integer, String> entry : entrySet) {
        System.out.println("键"+entry.getKey() + ":" + "值" + entry.getValue());
    }
}

get

@Test
public void get() {
    //get: 根据key获取value
    Map<Integer, String> map = new HashMap<>();
    map.put(1,"get method");
    System.out.println("map.get(1) = " + map.get(1));
}

getOrDefault

@Test
public void getOrDefault() {
    //getOrDefault: 返回指定key的值,如果不存在该key则返回defaultValue的值
    Map<Integer, String> map = new HashMap<>();
    map.put(1,"getOrDefault method");
    //不存在3号key,返回参数里指定的数据
    System.out.println(map.getOrDefault(3, "getOrDefault"));
}

isEmpty

@Test
public void isEmpty() {
    //isEmpty: 判断集合是否为空,非空返回true
    Map<Integer, String> map = new HashMap<>();
    //集合为空返回true
    System.out.println("map.isEmpty() = " + map.isEmpty());
}

keySet

@Test
public void keySet() {
    //keySet: 返回集合的key值
    Map<Integer, String> map = new HashMap<>();
    map.put(100,"KeySet");
    System.out.println("map.keySet() = " + map.keySet());
}

marge

@Test
public void marge() {
    //marge: 如果指定的键不存在则输出设置的value值,如果存在则用存在键中的值加merge参数里的值
    Map<Integer, String> map = new HashMap<>();
    map.put(1,"marge");
    System.out.println(map.merge(1, " + new Value", (k, v) -> (k + v)));
}

put

@Test
public void put() {
    //put: 设置一个键值对数据,键不可重复
    Map<Integer, String> map = new HashMap<>();
    map.put(1,"put1");
    map.put(2,"put2");
    Set<Map.Entry<Integer, String>> entries = map.entrySet();
    //使用entrySet方法遍历map集合
    for (Map.Entry<Integer, String> entry : entries) {
        String kv = "Key = " + entry.getKey() + " " + "Value = " + entry.getValue();
        System.out.println(kv);
    }
}

putAll

@Test
public void putAll() {
    //putAll: 根据一个map集合批量添加参数
    Map<Integer, String> map1 = new HashMap<>();
    Map<Integer, String> map2 = new HashMap<>();
    map2.put(1,"putAll method");
    map1.putAll(map2);
    //打印map1集合
    System.out.println(map1);
}

putIfAbsent

@Test
public void putIfAbsent() {
    //putIfAbsent: 如果存在指定的键则返回map集合的数据,否则返回null
    Map<Integer, String> map = new HashMap<>();
    map.put(1,"putIfAbsent");
    System.out.println(map.putIfAbsent(1, "putIfAbsent"));
}

remove

@Test
public void remove() {
    //remove: 移除指定的key
    Map<Integer, String> map = new HashMap<>();
    map.put(1,"remove method");
    //移除1号key
    System.out.println("map.remove(1) = " + map.remove(1));
}

remove(Object key, Object value)

@Test
public void removeTwoArgs() {
    //remove(Object key, Object value) :移除指定的键值对
    Map<Integer, String> map = new HashMap<>();
    map.put(1,"remove method");
    //移除指定的键值对
    System.out.println(map.remove(1, "remove method"));
}

replace

@Test
public void replace() {
    //replace: 如果存在指定的key则替换新的value
    Map<Integer, String> map = new HashMap<>();
    map.put(1,"replace");
    //替换键1的值为new Value
    System.out.println(map.replace(1, "new Value"));
    //此时打印为替换的字符串
    System.out.println(map);
}

replace(K key, V oldValue, V newValue)

@Test
public void replaceThreeArgs() {
    //replace(K key, V oldValue, V newValue): 根据key和旧value替换新的value
    Map<Integer, String> map = new HashMap<>();
    map.put(1,"replace");
    //指定旧value和新value
    System.out.println(map.replace(1, "replace", "new replace"));
    System.out.println("map = " + map);
}

replaceAll

@Test
public void replaceAll() {
    //replaceAll: 通过lambda批量添加
    Map<Integer, String> map = new HashMap<>();
    map.put(1,"replaceAll");
    map.replaceAll((k,v) -> "new" + "replaceAll" + "Lambda");
    System.out.println("map = " + map);
}

size

@Test
public void size() {
    //size: 返回map集合的数据长度
    Map<Integer, String> map = new HashMap<>();
    int size = map.size();
    System.out.println(size);
}

values

@Test
public void values() {
    //values: 返回所有数据s
    Map<Integer, String> map = new HashMap<>();
    map.put(1,"values method");
    Collection<String> values = map.values();
    System.out.println(values);
}

未经允许不得转载:搜云库技术团队 » Map集合方法API实例演示

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们