Java 值作为 HashMap 中对象的数组列表

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

Values as an arraylist of Objects in HashMap

javalistarraylisthashmap

提问by noobcoder

I want to create a HashMap that stores key as an Integer value which is the ID of a restaurant. The value should be a List of the Restaurant objects. But my IDE is not happy with the way I am doing it while adding the restaurant object to the List. Here is my code:

我想创建一个 HashMap 将键存储为整数值,即餐厅的 ID。该值应该是餐厅对象的列表。但是我的 IDE 对我在将餐厅对象添加到列表时的做法不满意。这是我的代码:

public List getTopPerformers(List<RestaurantInfo> restaurants){

    HashMap <Integer, List<RestaurantInfo>> map = new HashMap<Integer,
                                             List< RestaurantInfo>>();
             // Key is restaurant ID. Value is the Object of Class RestaurantInfo
    List<RestaurantInfo> ll;
    for(RestaurantInfo restaurant: restaurants){

        map.put(restaurant.cityId, ll.add(restaurant));

    }
}

My Restaurant class has properties as cityId, orderCount and restaurantId.

我的餐厅类具有 cityId、orderCount 和 restaurantId 等属性。

The map.put(restaurant.cityId, ll.add(restaurant));line gives error as follows and obviously it will never compile.

map.put(restaurant.cityId, ll.add(restaurant));行给出如下错误,显然它永远不会编译。

no suitable method found for put(int,boolean)
method HashMap.put(Integer,List<RestaurantInfo>) is not applicable

(actual argument boolean cannot be converted to List by method invocation conversion)

(不能通过方法调用转换将实参boolean转成List)

采纳答案by Akshay

ll.add(restaurant) returns boolean.

ll.add(restaurant) 返回布尔值。

So, when you do:

所以,当你这样做时:

map.put(restaurant.cityId, ll.add(restaurant));

you are trying to add (int, boolean) to a map of type: (Integer,List)

您正在尝试将 (int, boolean) 添加到类型为:(Integer,List) 的映射中

Also, below code will add all restaurants to every cityid:

此外,以下代码会将所有餐厅添加到每个 cityid:

List<RestaurantInfo> ll = new List<RestaurantInfo>();
for(RestaurantInfo restaurant: restaurants){
    ll.add(restaurant);
    map.put(restaurant.cityId, ll);
}

I think what you need is:

我认为你需要的是:

List<RestaurantInfo> ll;
for (RestaurantInfo restaurant: restaurants) {
  // If restaurant is from the same city which is present in the map then add restaurant to the existing list, else create new list and add.
  if (map.containsKey(restaurant.cityId)) {
    ll = map.get(restaurant.cityId);
  } else {
    ll = new List<RestaurantInfo>();
  }
  ll.add(restaurant);
  map.put(restaurant.cityId, ll);
}

回答by kosa

  map.put(restaurant.cityId, ll.add(restaurant));

In this statement,

在这份声明中,

ll.add(restaurant)

returnvalue for add operation is boolean, which is why you are getting that error.

return添加操作的值是boolean,这就是您收到该错误的原因。

What you may need to do would be something like:

您可能需要做的是:

ll.add(restaurant);
map.put(restaurant.cityId, ll);

回答by Sage

add(E)function of a collectionreturns boolean: trueIf the data Eis added and the collectionstructure has been changed (Returns falseif this collection does not permit duplicates and already contains the specified element).

add(E)集合的函数返回布尔值:true如果E添加了数据并且集合结构已更改(false如果此集合不允许重复并且已经包含指定的元素,则返回)。

Hence:

因此:

for(RestaurantInfo restaurant: restaurants){
        map.put(restaurant.cityId, ll.add(restaurant));
    }

is essentially equivalent to:

本质上等同于:

for(RestaurantInfo restaurant: restaurants){
            map.put(restaurant.cityId, boolean);
        }

So, First add the resutaurantinstances to list llone by one and then add lllist instance to the map.

因此,首先将resutaurant实例添加到列表中ll,然后将ll列表实例添加到map.

You might want to do something like this:

你可能想做这样的事情:

RestaurantInfo restaurant =  resturants.get(0);
int cityId = restaurant.cityId;

List<RestaurantInfo> ll = new ArrayList<>();

for(RestaurantInfo restaurant: restaurants){
            ll.add(restaurant);
        }

 map.put(cityId, ll);