【发布时间】: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<RestaurantInfo>()编译,假设它是java.util.List。 -
编辑帖子并将其删除。
-
现在
ll没有初始化,所以ll.add不会编译 - 例如使用new ArrayList<RestaurantInfo>()。
标签: java list arraylist hashmap