Java LinkedHashMap 中的重复项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19864136/
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 20:49:47  来源:igfitidea点击:

Duplicates in LinkedHashMap

javatreemaplinkedhashmap

提问by Ramin

In my code I am using a set of interleaved LinkedHashMaps inside each other as below. The code is fine and gives me the result I want except it automatically removes the duplicates. I couldnt find out how I can use TreeMap or Set in order to keep the duplicates.

在我的代码中,我使用了一组相互交错的 LinkedHashMap,如下所示。代码很好,给了我想要的结果,除了它会自动删除重复项。我不知道如何使用 TreeMap 或 Set 来保留重复项。

LinkedHashMap<String, LinkedHashMap<Integer, LinkedHashMap<String, Vector<String>>>> 
dataAll =new LinkedHashMap<String, LinkedHashMap<Integer, LinkedHashMap<String, 
Vector<String>>>>();

回答by KKKCoder

LinkedHashMap is still a Map data structure. It maps a unique key to a value. If you assign two different values to a key the second value will simply replace the first value assigned to that key.

LinkedHashMap 仍然是一种 Map 数据结构。它将唯一键映射到值。如果您为一个键分配两个不同的值,第二个值将简单地替换分配给该键的第一个值。

Also imagine why do you need a Map of duplicated key? The sole purpose of Map is to provide a one to one relationship between key/value pair. It does not handle one to many relationship.

还想象一下为什么你需要一个重复键的映射?Map 的唯一目的是提供键/值对之间的一对一关系。它不处理一对多的关系。

If you have to map a key with a list of values, use something like:

如果您必须使用值列表映射键,请使用以下内容:

LinkedHashMap<String, List<..>>

This allows you to have one key maps to a list of values.

这允许您将一个键映射到一系列值。