Multiple values for a key in HashMap in Java(Java中HashMap中键的多个值)
问题描述
是否可以在 HashMap 中保留与键对应的多个值?如果是,怎么做?
Is it possible to keep multiple values corresponding to a key in a HashMap? If yes, how?
推荐答案
是的,这叫做链接.您将希望尽可能避免链接,尤其是当链的大小开始增加时.更长的链大小将违背使用哈希结构的全部目的,因为目标是尽可能接近 O(1).
Yes, this is called chaining. You will want to avoid chaining as much as possible, especially if the size of the chain starts increasing. Longer chain size will counter the whole purpose of using a hash structure because the goal is to come as close to O(1) as possible.
Map<String, List<String>> hm = new HashMap<String, List<String>>();
List<String> values = new ArrayList<String>();
values.add("Value 1");
values.add("Value 2");
hm.put("Key1", values);
这篇关于Java中HashMap中键的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java中HashMap中键的多个值


- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01