用重复键存储值在TreeMap,HashMap或LinkedHashMap中 [英] Storing values with duplicate keys in TreeMap, HashMap, or LinkedHashMap

查看:214
本文介绍了用重复键存储值在TreeMap,HashMap或LinkedHashMap中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从一个项目中获取有关社会安全网站名称的数据。基本上我给了一个数字x,y和z年。我必须返回从y到z的每个年份的前x个名字。

因此,网站返回的数据是名称,排名和年份。我必须输入返回到TreeMap,HashMap或LinkedHashMap中的每个名称,但我不知道如何存储它们,因为不管我用什么作为关键字,都可能有重复。这一年不可能是关键,因为我每年都会获得顶级x的名字,所以他们都会来自同一年。如果有多年,那么会有几个名字等级1等,因为每年有一个名字,所以这不可能是关键。而这个名字本身并不是关键,因为同一个名字可能已经在多年的前几名名单中。



我已经设法理解了大多数复杂这个项目的一部分,但这 - 最简单的部分之一,我似乎无法理解!



我听说过我可以使用类似的东西作为关键的一年,使价值列表的名称或类似的名单,但我不知道我将如何在像这样的实现中添加值。我非常感谢任何建议!



非常感谢。



编辑:请注意,我必须使用TreeMap,HashMap或LinkedHashMap。我听说过MultiMap,但这不是我的选择之一。我认为使用散列表与 List 就是你特别要求的。如何实例化这样一个对象的例子是:

  HashMap< Integer,List< String>> myHashMap = new HashMap< Integer,List< String>>(); 

请注意,我们必须使用整数,因为散列图仅适用于对象。为此增加值,你可以这样做:

  myHashMap.get([whatever you want to]]。add([[无论你想要什么名字]);不过,看看这个问题显示这不会像这样简单,因为你必须实例化每个List你所有的钥匙(这个问题都是专门处理多维哈希的,但前提是一样的)。但是,这是可行的,因为这个问题的答案表明了这一点。你应该看看它,因为我认为它可以帮助你理解这一切发生了什么,但可能适用于你的代码可能看起来像(几乎直接从链接问题的答案中获得): myHashMap.put(myYear,new List< String>() ); 

编辑:如果您无法使用列表里面的任何一个,我想你可以把另一个HashMap放在里面,但是我没有看到它有很多真正的用途,除非它是一个任意的需求。


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.

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.

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.

解决方案

I think using a hashmap with an List is 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:

myHashMap.get([whatever year you wanted]).add("[whatever name you want]");

However, a look at this question shows 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):

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.

这篇关于用重复键存储值在TreeMap,HashMap或LinkedHashMap中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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