我如何在Java中使用重复键创建HashMap? [英] How can i have a HashMap in Java with duplicate keys?

查看:247
本文介绍了我如何在Java中使用重复键创建HashMap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



所以我决定使用一个来存储一个对,一个键和一个值。 HashMap< String,String> ,但我注意到当我尝试添加一个已经存在的键时,前一个键(键值)被新键替换(NewKey,NewValue)入口丢失。所以当我有重复的键时,以前的键被替换为新的键。



如何使用重复键的HashMap?

解决方案

您需要 MultiMap
查看 Google Guava Multimap


I need a collection in Java that is going to store a pair, a key and a value.

So I decided to use a HashMap<String,String>, but I noticed that when I try to add a key that already exists, the previous (key,value) is replaced by the new one (NewKey,NewValue) and the previous entry is lost. So when I have duplicate keys, the previous key is replaced by the new one.

How can I have a HashMap with duplicate keys?

解决方案

you need MultiMap, take a look at Google Guava Multimap

这篇关于我如何在Java中使用重复键创建HashMap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆