【发布时间】:2016-06-19 11:16:24
【问题描述】:
我不明白为什么我的地图值没有增加!!!
<% Collection<Product> items = basket.getItems();
HashMap<Product,Integer> itemMap = new HashMap<Product,Integer>();
for(Product p : items){
if(!itemMap.containsKey(p)){
itemMap.put(p,1);}
else{itemMap.put(p, itemMap.get(p) + 1);}
out.println("<p>"+itemMap+"</p>");
}%>
每次都是1!
【问题讨论】:
-
最可能的原因是
Product没有覆盖hashCode和equals -
能分享一下
Product的实现吗? -
可能的解释是您的集合中没有任何重复的产品。收藏中是否有
equals另一个产品?另外,请不要使用小脚本。将该代码放入 servlet。
标签: java dictionary foreach