news 2026/6/7 12:29:14

Java中Map的多种用法

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Java中Map的多种用法

1. 创建:5 种一行写法

// ① 传统 Map<String, Integer> map = new HashMap<>(); // ② 不可变(JDK 9+) Map<String, Integer> map = Map.of("a", 1, "b", 2); // ③ 函数式(JDK 8+) Map<String, Integer> map = Stream.of(new Object[][]{{"a", 1}, {"b", 2}}) .collect(Collectors.toMap(o -> (String) o[0], o -> (Integer) o[1])); // ④ Guava 可变 Map<String, Integer> map = Maps.newHashMap(); // ⑤ Guava 不可变 Map<String, Integer> map = ImmutableMap.of("a", 1, "b", 2);

2. 读写:日常 API

map.put("k", 1); // 存 map.get("k"); // 取 map.getOrDefault("k", 0); // 防 NPE map.remove("k"); // 删 map.containsKey("k"); // 判断 map.size(); // 大小

3. 遍历:4 种姿势

// ① entrySet(最快) for (Map.Entry<String, Integer> e : map.entrySet()) { System.out.println(e.getKey() + "=" + e.getValue()); } // ② Java 8 Lambda map.forEach((k, v) -> System.out.println(k + "=" + v)); // ③ keySet for (String k : map.keySet()) { System.out.println(k + "=" + map.get(k)); } // ④ Stream map.entrySet().stream() .filter(e -> e.getValue() > 10) .forEach(e -> System.out.println(e.getKey()));

4. 计算型 Map:merge / compute

// 计数器:单词出现次数 map.merge(word, 1, Integer::sum); // 累加:key 对应的值 + delta map.compute(key, (k, v) -> v == null ? delta : v + delta);

5. 线程安全:3 种锁策略

// ① 全表锁(慢) Map<String, Integer> map = new Hashtable<>(); // ② 分段锁(快) Map<String, Integer> map = new ConcurrentHashMap<>(); // ③ 不可变(无锁) Map<String, Integer> map = ImmutableMap.of("a", 1);

6. 顺序 Map:3 种有序实现

// ① 插入顺序 Map<String, Integer> map = new LinkedHashMap<>(); // ② 访问顺序(LRU) Map<String, Integer> map = new LinkedHashMap<>(16, 0.75f, true); // ③ 排序顺序 Map<String, Integer> map = new TreeMap<>(); // 自然序 Map<String, Integer> map = new TreeMap<>(Comparator.reverseOrder()); // 倒序

7. 空值友好:Optional 链

String name = Optional.ofNullable(map.get("k")) .map(String::valueOf) .orElse("");

8. 黑科技:Map 当作函数缓存

Map<String, Function<Integer, Integer>> funcMap = Map.of( "square", x -> x * x, "cube", x -> x * x * x ); int result = funcMap.getOrDefault("square", x -> x).apply(5); // 25

9. 一行记忆

“HashMap 日常,LinkedHashMap 顺序,TreeMap 排序,ConcurrentHashMap 并发,ImmutableMap 只读,merge 计数,Optional 防空!”

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/6 10:33:14

DAY25 pipeline管道

浙大疏锦行 # 导入基础库 import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns import time # 导入 time 库 import warnings# 忽略警告 warnings.filterwarnings("ignore")# 设置中文字体和负号正常显示 plt.rcParams[…

作者头像 李华
网站建设 2026/6/7 14:27:38

深入理解连接错误:从 “ld returned 1“到系统性解决方案

引言 在C/C程序的构建流程中&#xff0c;链接&#xff08;Linking&#xff09; 是将多个预编译目标文件&#xff08; “.o”/ “.obj”&#xff09;与库文件&#xff08; “.a”/ “.lib”、 “.so”/ “.dll”&#xff09;组合为最终可执行文件或动态库的核心阶段。相较于编译…

作者头像 李华
网站建设 2026/6/6 12:12:51

人工智能之编程基础 Python 入门

前言本章节讲述python的基础数据类型&#xff0c;python的基础数据类型主要包括以下​不可变数据&#xff08;3 个&#xff09;&#xff1a;​Number&#xff08;数字&#xff09;、String&#xff08;字符串&#xff09;、Tuple&#xff08;元组&#xff09;&#xff1b;​可变…

作者头像 李华
网站建设 2026/6/6 12:49:35

GraphQL Editor性能优化实战:5大策略应对大规模Schema挑战

GraphQL Editor性能优化实战&#xff1a;5大策略应对大规模Schema挑战 【免费下载链接】graphql-editor &#x1f4fa; Visual Editor & GraphQL IDE. 项目地址: https://gitcode.com/gh_mirrors/gr/graphql-editor 在处理日益复杂的GraphQL项目时&#xff0c;Sche…

作者头像 李华
网站建设 2026/6/6 14:36:18

8B参数如何超越GPT-4o?揭秘MiniCPM-V 4.5的部署实战

8B参数如何超越GPT-4o&#xff1f;揭秘MiniCPM-V 4.5的部署实战 【免费下载链接】OmniLMM 项目地址: https://gitcode.com/gh_mirrors/om/OmniLMM 你是否曾想过&#xff0c;一个仅有8B参数的开源模型竟然能在多项基准测试中超越GPT-4o-latest这样的顶级闭源模型&#x…

作者头像 李华