简体   繁体   English

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

[英]Values as an arraylist of Objects in HashMap

I want to create a HashMap that stores key as an Integer value which is the ID of a restaurant.我想创建一个 HashMap 将键存储为整数值,即餐厅的 ID。 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.但是我的 IDE 对我在将餐厅对象添加到列表时的做法不满意。 Here is my code:这是我的代码:

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)); map.put(restaurant.cityId, ll.add(restaurant)); line gives error as follows and obviously it will never compile.行给出如下错误,显然它永远不会编译。

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)

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);
}
  map.put(restaurant.cityId, ll.add(restaurant));

In this statement,在这份声明中,

ll.add(restaurant)

return value for add operation is boolean , which is why you are getting that error. add 操作的return值是boolean ,这就是您收到该错误的原因。

What you may need to do would be something like:您可能需要做的是:

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

add(E) function of a collection returns boolean: true If the data E is added and the collection structure has been changed (Returns false if 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 resutaurant instances to list ll one by one and then add ll list instance to the map .因此,首先将resutaurant实例添加到 list ll ,然后将ll list instance 添加到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);

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

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