[英]Anyway to have two of the same keys, but different values in Java HashMap?
I'm using a HashMap to contain a misspelled word, and a int array that contains its offset in the document, the line number it is on, and the length of that misspelled word. 我正在使用HashMap来包含一个拼写错误的单词,以及一个int数组,该数组包含其在文档中的偏移量,它所在的行号以及该拼写错误的单词的长度。 By default, HashMap replaces the old values if the key is already contained.
默认情况下,如果密钥已包含,则HashMap替换旧值。 I would like to keep those values, and have duplicate keys.
我想保留这些值,并具有重复的键。
Is there anyway to accomplish this? 反正有做到这一点吗?
You CANNOT have duplicate keys in Map
but you can create a MultiMap ie A map to associate a single key with multiple values. 您在
Map
不能有重复的键,但是您可以创建一个MultiMap,即将单个键与多个值关联的Map。 Something like this: 像这样:
Map<String,ArrayList<String>> myMap = new HashMap<String,ArrayList<String>>();
or you can use readymade multi-map implementations such as: 或者您可以使用现成的多地图实现,例如:
No, a HashMap is always uniquely keyed. 不,HashMap始终是唯一键。 If you need aap that a key can have multiple values, look at Guava's MultiMap
如果需要一个键可以具有多个值的信息,请查看番石榴的MultiMap
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.