如何存储具有相同密钥的散列表中的多个对象? [英] How to store multiple objects from a hashmap that has the same key?
问题描述
编辑
我试过这个>具有多个值的HashMap在同一个键下,我的hashMap现在看起来像这样 HashMap< String,List< Place>> ; placeMap = new HashMap<>();
还试图把Object放在Place上(放置是我的超类)。但是,当我现在创建我的子类并想将它们添加到HashMap时,我会得到:
方法put(String,List)类型
HashMap< String,List< Place>>
不适用于参数(String,NamedPlace)
和
方法put(String,List)的类型为
HashMap< String,List< Place>>
不适用于参数(字符串,DescPlace)是我的添加,它创建了错误:
pre>NamedPlace p = new NamedPlace(x,y,answer,col,cat);
placeMap.put(answer,p);
DescPlace dp = new DescPlace(x,y,answer,desc,col,cat);
mp.add(dp);
placeMap.put(answer,dp);
NamedPlace和DescPlace都是Place的子类,我希望它们都在相同的HashMap中。
OP
我正在研究一个小项目。问题是我需要在项目的这个部分使用HashMap而不是ArrayList,因为HashMap对于搜索来说快很多。我创建了一个像这样的HashMap:
HashMap< String,Object> placeMap = new HashMap<>();
字符串是对象的名称,但事情是多个对象可以拥有一样的名字。所以我在我的搜索字段中搜索一个对象,并且我想将所有那些具有该名称的对象存储到一个ArrayList中,这样我就可以在它们中更改信息。
对象有很多不同的值,如名称,位置,一些布尔值等。
我需要在我的对象类中创建一个HashCode方法,它应该创建一个唯一的哈希码?
使用标准Map< String,List< YourClassHere>>
解决方案 >实例,重要的是要记住,每个条目的映射值都是List< YourClassHere>
,并且不会以任何特殊方式处理它。所以在你的情况下,如果你有
私人地图< String,List< Place>> placeMap = new HashMap<>();
然后存储您需要执行的值,如下所示:
NamedPlace p = new NamedPlace(x,y,answer,col,cat);
列表< Place> list = placeMap.get(answer);
list.add(p);
然而,这段代码有一些基本问题。
- 没有考虑到
answer 可能不存在于 placeMap
。
- 假设总是有一个
/ p>List< Place>
每个键你查询。
所以解决这些潜在问题的最佳方法是做如下(Java 7和更高版本):
NamedPlace p = new NamedPlace(x,y,answer,col,cat);
if(placeMap.containsKey(answer)&& placeMap.get(answer)!= null){
placeMap.get(answer).add(p);
} else {
列表< Place> list = new ArrayList< Place> (); // ..或者无论你需要什么List实现
list.add(p);
placeMap.put(answer,list);
}
如果您想扫描地点列表,代码会看起来像这个:
if(placeMap.containsKey(key)&& placeMap.get(answer)!= null){$ (Place p:placeMap.get(key)){
$ p
//做东西
}
}
p>EDIT
I've tried this HashMap with multiple values under the same key, and my hashMap now looks like this
HashMap<String, List<Place>> placeMap = new HashMap<>();
Also tried to put Object instead of Place(place is my superclass). But when I now create my subclasses and wants to add them to the HashMap I get:
The method put(String, List) in the type
HashMap<String,List<Place>>
is not applicable for the arguments (String, NamedPlace)and
The method put(String, List) in the type
HashMap<String,List<Place>>
is not applicable for the arguments (String, DescPlace)here is my adding which created the error:
NamedPlace p = new NamedPlace(x,y,answer,col,cat); placeMap.put(answer, p);
DescPlace dp = new DescPlace(x,y,answer, desc, col, cat); mp.add(dp); placeMap.put(answer, dp);
NamedPlace and DescPlace are both subclasses to Place, and I want them both in the same HashMap..
OP
I'm working on a little project here. The thing is that I need to use a HashMap instead of a ArrayList on this part of the project because HashMap is alot faster for searching. I've created a HashMap like this:
HashMap<String, Object> placeMap = new HashMap<>();
The String is the name of the Object, but the thing is that more than one object can have the same name. So I search for a object in my searchfield and I want to store all those objects that has that name into an ArrayList so I can change info in just them.
The object have alot of different values, like name, position, some booleans etc.
Do I need to create a HashCode method into my object class which shall create a unique hashcode?
解决方案When using a standard
Map<String, List<YourClassHere>>
instance, it is important to remember that the map's values for each entry will be aList<YourClassHere>
, and will not handle it in any special way. So in your case, if you haveprivate Map<String, List<Place>> placeMap = new HashMap<>();
Then to store values you will need to do as follows:
NamedPlace p = new NamedPlace(x,y,answer,col,cat); List<Place> list = placeMap.get (answer); list.add(p);
However, this piece of code has some underlying problems.
- It doesn't take into account that
answer
might not be present inplaceMap
.- It assumes that there's always a
List<Place>
instance for each key you query.So the best way to fix those potential problems is to do as follows (Java 7 and later):
NamedPlace p = new NamedPlace(x,y,answer,col,cat); if (placeMap.containsKey (answer) && placeMap.get (answer) != null) { placeMap.get (answer).add(p); } else { List<Place> list = new ArrayList<Place> (); // ..or whatever List implementation you need list.add (p); placeMap.put (answer, list); }
If you want to scna through the list of places, the code would look like this:
if (placeMap.containsKey (key) && placeMap.get (answer) != null) { for (Place p: placeMap.get (key)) { // Do stuff } }
这篇关于如何存储具有相同密钥的散列表中的多个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文