【问题标题】:Values as an arraylist of Objects in HashMapHashMap 中作为对象数组列表的值
【发布时间】:2013-11-10 22:28:30
【问题描述】:

我想创建一个 HashMap,将键存储为整数值,即餐厅的 ID。该值应该是 Restaurant 对象的 List。但是在将餐厅对象添加到列表时,我的 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));

    }
}

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

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

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

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

【问题讨论】:

  • 我不希望 new List&lt;RestaurantInfo&gt;() 编译,假设它是 java.util.List
  • 编辑帖子并将其删除。
  • 现在ll 没有初始化,所以ll.add 不会编译 - 例如使用new ArrayList&lt;RestaurantInfo&gt;()

标签: java list arraylist hashmap


【解决方案1】:

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

所以,当你这样做时:

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

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

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

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

我认为你需要的是:

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);
}

【讨论】:

    【解决方案2】:
      map.put(restaurant.cityId, ll.add(restaurant));
    

    在此声明中,

    ll.add(restaurant)
    

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

    您可能需要做的是:

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

    【讨论】:

    • 我得到了错误原因。我尝试了上述更改。您不认为这会简单地将所有餐厅对象添加到列表中。我希望将 key 作为 cityId,其对应的值应该是仅适用于该 cityId 的餐厅对象列表。所以本质上,每个新的 cityId 都会有一个新的列表作为值附加到它上面。
    • 那么你的list声明应该是简单的List./
    【解决方案3】:
    collection

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

    因此:

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

    本质上等同于:

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

    所以,首先将resutaurant 实例添加到ll 列表中,然后将ll 列表实例添加到map 中。

    你可能想做这样的事情:

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-31
      • 2015-06-17
      相关资源
      最近更新 更多