Java 如何在HashMap中包含重复的键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18922165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:32:07  来源:igfitidea点击:

How to include duplicate keys in HashMap?

java

提问by LoneWolf

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??

我需要保留顺序的重复键。有什么方法可以做到这一点??

回答by Luiggi Mendoza

Use Map<Integer, List<String>>:

使用Map<Integer, List<String>>

Map<Integer, List<String>> map = new LinkedHashMap< Integer, List<String>>();

map.put(-1505711364, new ArrayList<>(Arrays.asList("4")));
map.put(294357273, new ArrayList<>(Arrays.asList("15", "71")));
//...

To add a new key/value pair in this map:

要在此映射中添加新的键/值对:

public void add(Integer key, String newValue) {
    List<String> currentValue = map.get(key);
    if (currentValue == null) {
        currentValue = new ArrayList<String>();
        map.put(key, currentValue);
    }
    currentValue.add(newValue);
}

回答by Rohit Jain

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.

.a 文件中不能有重复的键Map。您可以创建一个Map<Key, List<Value>>,或者如果可以,使用Guava 的Multimap.

Multimap<Integer, String> multimap = ArrayListMultimap.create();
multimap.put(1, "rohit");
multimap.put(1, "jain");

System.out.println(multimap.get(1));  // Prints - [rohit, jain]

And then you can get the java.util.Mapusing the Multimap#asMap()method.

然后你可以得到java.util.Map使用Multimap#asMap()方法。

回答by Subhrajyoti Majumder

Mapdoes not supports duplicate keys. you can use collection as value against same key.

Map不支持重复键。您可以使用集合作为针对同一键的值。

Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value.

将指定值与此映射中的指定键相关联(可选操作)。如果映射先前包含键的映射,则旧值将替换为指定值。

Documentation

文档

you can use any kind of Listor Setimplementation according to your requirement.

您可以根据您的要求使用任何类型 ListSet实现。

If your values might be also duplicate you can go with ArrayListor LinkedList, in case values are unique you can use HashSetor TreeSetetc.

如果你的价值观可能也复制你可以去ArrayList或者LinkedList,如果值是唯一可以使用HashSetTreeSet等。



Also In google guava collection libraryMultimapis available, it is a collection that maps keys to values, similar to Map, but in which each key may be associated with multiple values. You can visualize the contents of a multimap either as a map from keys to nonempty collections of values:

另外在谷歌番石榴集合库Multimap中可用,它是一个将键映射到值的集合,类似于Map,但其中每个键可能与多个值相关联。您可以将 multimap 的内容可视化为从键到非空值集合的映射:

a → 1, 2
b → 3  

Example -

例子 -

ListMultimap<String, String> multimap = ArrayListMultimap.create();
multimap.put("a", "1");
multimap.put("a", "2");
multimap.put("c", "3");

回答by BlackHatSamurai

hashMapscan't have duplicate keys. That said, you can create a map with list values:

hashMaps不能有重复的键。也就是说,您可以使用列表值创建地图:

Map<Integer, List<String>>

However, using this approach will have performance implications.

但是,使用这种方法会对性能产生影响。