java map<String,List>判断是否有key,get(key.add(x),否则put(key,new list(){x})的新写法

在Java中,你可以通过几种方式实现这个逻辑。以下是几种常见的实现方法:

方法1:传统方式(使用 containsKey)

复制代码
Map<String, List<String>> map = new HashMap<>();

public void addValue(String key, String value) {
    if (map.containsKey(key)) {
        map.get(key).add(value);
    } else {
        List<String> newList = new ArrayList<>();
        newList.add(value);
        map.put(key, newList);
    }
}

方法2:使用 getOrDefault(Java 8+)

复制代码
Map<String, List<String>> map = new HashMap<>();

public void addValue(String key, String value) {
    List<String> list = map.getOrDefault(key, new ArrayList<>());
    list.add(value);
    map.put(key, list);
}

方法3:使用 computeIfAbsent(推荐,Java 8+)

复制代码
Map<String, List<String>> map = new HashMap<>();

public void addValue(String key, String value) {
    map.computeIfAbsent(key, k -> new ArrayList<>()).add(value);
}

方法4:使用 putIfAbsent

复制代码
Map<String, List<String>> map = new HashMap<>();

public void addValue(String key, String value) {
    map.putIfAbsent(key, new ArrayList<>());
    map.get(key).add(value);
}

完整示例

复制代码
import java.util.*;

public class MapExample {
    public static void main(String[] args) {
        // 使用方法3:computeIfAbsent(最简洁)
        Map<String, List<String>> map = new HashMap<>();
        
        addToMap(map, "fruit", "apple");
        addToMap(map, "fruit", "banana");
        addToMap(map, "vegetable", "carrot");
        addToMap(map, "fruit", "orange");
        
        System.out.println(map);
        // 输出: {vegetable=[carrot], fruit=[apple, banana, orange]}
    }
    
    // 使用 computeIfAbsent
    public static void addToMap(Map<String, List<String>> map, String key, String value) {
        map.computeIfAbsent(key, k -> new ArrayList<>()).add(value);
    }
}

针对不同类型 List 的通用方法

如果你需要处理不同类型的数据,可以使用泛型:

复制代码
public class MapUtils {
    // 通用方法
    public static <K, V> void addToListInMap(Map<K, List<V>> map, K key, V value) {
        map.computeIfAbsent(key, k -> new ArrayList<>()).add(value);
    }
    
    // 使用示例
    public static void main(String[] args) {
        Map<String, List<Integer>> intMap = new HashMap<>();
        Map<String, List<Double>> doubleMap = new HashMap<>();
        Map<String, List<Object>> objectMap = new HashMap<>();
        
        addToListInMap(intMap, "scores", 100);
        addToListInMap(doubleMap, "prices", 19.99);
        addToListInMap(objectMap, "items", "item1");
    }
}

线程安全版本

如果需要在多线程环境中使用:

复制代码
Map<String, List<String>> concurrentMap = new ConcurrentHashMap<>();

public void addValueSafely(String key, String value) {
    // 使用 ConcurrentHashMap 的 computeIfAbsent
    concurrentMap.computeIfAbsent(key, k -> 
        Collections.synchronizedList(new ArrayList<>())
    ).add(value);
}

推荐

推荐使用方法3(computeIfAbsent),因为:

  1. 代码最简洁

  2. 避免多次访问Map

  3. 性能较好

  4. 是原子操作(在多线程环境下更安全)

如果你使用的是Java 8之前的版本,可以使用方法1,虽然代码稍长,但兼容性最好。

相关推荐
Coder_Boy_5 分钟前
Deeplearning4j+ Spring Boot 电商用户复购预测案例
java·人工智能·spring boot·后端·spring
踢足球092913 分钟前
寒假打卡:2026-2-7
java·开发语言·javascript
闻哥16 分钟前
Kafka高吞吐量核心揭秘:四大技术架构深度解析
java·jvm·面试·kafka·rabbitmq·springboot
金牌归来发现妻女流落街头18 分钟前
【Springboot基础开发】
java·spring boot·后端
考琪34 分钟前
Nginx打印变量到log方法
java·运维·nginx
wangjialelele44 分钟前
Linux中的进程管理
java·linux·服务器·c语言·c++·个人开发
历程里程碑1 小时前
普通数组----轮转数组
java·数据结构·c++·算法·spring·leetcode·eclipse
晔子yy1 小时前
如何设计让你的程序同时处理10w条数据
java
Yvonne爱编码1 小时前
链表高频 6 题精讲 | 从入门到熟练掌握链表操作
java·数据结构·链表
lpfasd1231 小时前
物联网后端岗位java面试题
java·物联网·php