如何在HashMap中包含重复的键? [英] How to include duplicate keys in HashMap?
本文介绍了如何在HashMap中包含重复的键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要键集中的链接和多个键。我试过这个:
I need linked and multiple keys in key set. I tried this:
LinkedHashMap<Integer, String> map = new LinkedHashMap< Integer,String>();
map.put( -1505711364,"4");
map.put(294357273, "15"); map.put(-1593134417, "28"); map.put(-1231165758, "45");
map.put(121046798, "58");
map.put(294357273, "71"); map.put(-1593134417, "82"); map.put(-1231165758, "95");
map.put(121046798, "108");
我需要重复的密钥才能保留订单。这样做的方法是什么?
I need duplicate keys which is order preserved. What is the way to do this??
推荐答案
地图中不能有重复的键
。您可以创建一个 Map< Key,List< Value>>
,或者,如果可以,请使用 Guava的 Multimap
。
You can't have duplicate keys in a Map
. You can rather create a Map<Key, List<Value>>
, or if you can, use Guava's Multimap
.
Multimap<Integer, String> multimap = ArrayListMultimap.create();
multimap.put(1, "rohit");
multimap.put(1, "jain");
System.out.println(multimap.get(1)); // Prints - [rohit, jain]
然后你可以得到 java.util.Map
使用 Multimap#asMap()
方法。
And then you can get the java.util.Map
using the Multimap#asMap()
method.
这篇关于如何在HashMap中包含重复的键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文