Java中HashMap中键的多个值

Multiple values for a key in HashMap in Java(Java中HashMap中键的多个值)

本文介绍了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中键的多个值

基础教程推荐