简体   繁体   English

如何从具有相同键的哈希图中存储多个对象?

[英]How to store multiple objects from a hashmap that has the same key?

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<>(); 我已经在同一个键下尝试了具有多个值的HashMap ,并且我的hashMap现在看起来像这样: 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: 但是,当我现在创建子类并将其添加到HashMap时,我得到:

The method put(String, List) in the type HashMap<String,List<Place>> is not applicable for the arguments (String, NamedPlace) HashMap<String,List<Place>>类型的方法put(String,List)不适用于参数(String,NamedPlace)

and

The method put(String, List) in the type HashMap<String,List<Place>> is not applicable for the arguments (String, DescPlace) HashMap<String,List<Place>>类型的方法put(String,List)不适用于参数(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.. NamedPlace和DescPlace都是Place的子类,我希望它们都在同一HashMap中。

OP 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. 事实是,我需要在项目的这一部分使用HashMap而不是ArrayList,因为HashMap的搜索速度更快。 I've created a HashMap like this: 我已经创建了一个像这样的HashMap:

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. 因此,我在搜索字段中搜索一个对象,我想将所有具有该名称的对象存储到ArrayList中,以便仅更改它们中的信息。

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? 我是否需要在对象类中创建一个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 a List<YourClassHere> , and will not handle it in any special way. 使用标准Map<String, List<YourClassHere>>实例时,请务必记住,每个条目的映射值将是List<YourClassHere> ,并且不会以任何特殊方式处理它。 So in your case, if you have 因此,如果您有

private 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 in placeMap . 它没有考虑到answer可能不会出现在placeMap
  • It assumes that there's always a List<Place> instance for each key you query. 假定您查询的每个键始终有一个List<Place>实例。

So the best way to fix those potential problems is to do as follows (Java 7 and later): 因此,解决这些潜在问题的最佳方法是执行以下操作(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 {
    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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM