Java 在 TreeMap、HashMap 或 LinkedHashMap 中存储具有重复键的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20463525/
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
Storing values with duplicate keys in TreeMap, HashMap, or LinkedHashMap
提问by
I am currently working on a project in which I am retrieving data about names from the Social Security website. Basically I'm given a number x, and years y and z. I have to return the top x names from each of the years y through z.
我目前正在开展一个项目,在该项目中我正在从社会保障网站检索有关姓名的数据。基本上我得到了一个数字 x,以及年份 y 和 z。我必须返回从 y 到 z 的每一年的前 x 个名称。
So the data returned from the website is a name, a rank, and a year. I have to enter each name returned into either a TreeMap, HashMap, or LinkedHashMap, but I'm not sure how to store them, because no matter what I use as the key, there might be duplicates. The year cannot be the key as I will have the top x names from each year, so they would all be from the same year. If there are multiple years, there would be several names of rank 1, etc, as there is one for each year, so that could not be the key. And the name itself could not be the key as the same name might have been in the top several names for multiple years.
所以从网站返回的数据是一个名字、一个等级和一个年份。我必须将返回的每个名称输入到 TreeMap、HashMap 或 LinkedHashMap 中,但我不确定如何存储它们,因为无论我使用什么作为键,都可能存在重复项。年份不能是关键,因为我每年都会有前 x 个名字,所以它们都来自同一年。如果有多个年份,就会有几个排名第一的名字,等等,因为每年都有一个,所以这不是关键。并且名称本身不能成为关键,因为同一个名称可能多年来一直处于前几个名称中。
I've managed to understand most of the complicated parts of this project, yet this--one of the simplest parts, I can't seem to understand!
我已经设法理解了这个项目的大部分复杂部分,但是这个——最简单的部分之一,我似乎无法理解!
I've heard of ways that I can use something like the year as the key and make the value a list of names or something similar, but I'm not sure how I would add values in implementations like that. I would greatly appreciate any recommendations!
我听说过可以使用诸如年份之类的东西作为键并使值成为名称列表或类似内容的方法,但我不确定如何在这样的实现中添加值。我将不胜感激任何建议!
Thanks so much.
非常感谢。
Edit: Please note that I was specifically told I MUST use TreeMap, HashMap, or LinkedHashMap. I've heard of MultiMap but that's not one of my options.
编辑:请注意,有人特别告诉我必须使用 TreeMap、HashMap 或 LinkedHashMap。我听说过 MultiMap 但这不是我的选择之一。
采纳答案by Sammaron
I think using a hashmap with an Listis what you're specifically asking for. An example of how to instantiate such an object would be:
我认为使用带有列表的哈希图是您特别要求的。如何实例化此类对象的示例是:
HashMap<Integer, List<String>> myHashMap = new HashMap<Integer, List<String>>();
Note that we have to use Integer
because Hashmaps only work with objects. To add values to this, you could do:
请注意,我们必须使用,Integer
因为 Hashmaps仅适用于对象。要为此添加值,您可以执行以下操作:
myHashMap.get([whatever year you wanted]).add("[whatever name you want]");
However, a look at this questionshows this would not be quite as easy as this, as you must instantiate each List for all your key's (that question deals specifically with multidimensional hashmaps, but the premise is the same). However, it is doable, as the answer to that question demonstrates. You should have a look at it, as I think it will you help you understand what's going on with all this, but the code that might work for you could look like (taken almost directly from the answer to the linked question):
但是,查看此问题表明这不会像这样容易,因为您必须为所有键实例化每个 List(该问题专门涉及多维哈希图,但前提是相同的)。但是,正如该问题的答案所表明的那样,这是可行的。你应该看看它,因为我认为它会帮助你理解这一切是怎么回事,但可能对你有用的代码可能看起来像(几乎直接取自链接问题的答案):
if (!myHashMap.containsKey(myYear)) {
myHashMap.put(myYear, new List<String>());
}
Edit:If you can't use the List inside either, I suppose you could put another hashmap inside, but I don't see that having much real use for this unless it's just an arbitrary requirement.
编辑:如果你也不能在里面使用 List,我想你可以在里面放另一个哈希图,但我认为这没有太多实际用途,除非它只是一个随意的要求。