学习中遇到的HashMap问题
前言
今天在老师教Session的时候,利用了Session可持久化保存服务器端的特性尝试做了一下用HashMap嵌套的购物车(没有将购物车的商品信息保存在数据库中),之所以做的这么麻烦是为了巩固之前学习的Map知识和锻炼逻辑能力,正好也在其中遇到了一个关于HashMap 的问题,在此做个小小的记录,方便日后查看。
问题
服务器端保存购物车商品信息用的是HashMap嵌套,内层HashMap存放的是商品和该商品的数量,内层的HashMap中只存放一组键值对,外层HashMap存放的是商品和该商品总价,根据页面传过来的商品id在数据库中获取到商品的信息,。再遍历HashMap根据id判断是否已经存在该商品,再针对不同情况进行处理,所遇到的问题是购物车存在页面穿进来的该商品,那么我如果修改内层Map的Integer(数量),再修改外层HashMap的Value(该商品的总价),就会出现空指针异常,解决方案是先将商品总价保存起来,把内层HashMap从外层HashMap中remove掉,再修改商品数量,再将修改数量后的内层HashMap添加到外层HashMap中,代码如下:
<%@ page import="java.util.List" pageEncoding="utf-8" %> <%@ page import="model.Shop" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>购物</title> <script src="js/jquery-3.3.1.min.js"></script> </head> <body> <form action="shopcart" id="form"> <input type="hidden" id="count" name="count" value=""> <input type="hidden" id="id" name="goodid" value=""> <table> <tr> <th>店铺码</th> <th>水果名</th> <th>价格</th> <th>类别</th> <th>操作</th> </tr> <% List<Shop> list = (List<Shop>) request.getAttribute("shoplist"); for (int i=0;i<list.size();i++){ Shop shop = list.get(i); %> <tr> <td><%=shop.getCode()%></td> <td><%=shop.getName()%></td> <td><%=shop.getPrice()%></td> <td><%=shop.getType()%></td> <td> <%--<a href="shopcart?goodid="--%> <a href="javascript:void(0)" onclick="addToCart(<%=shop.getSid()%>)">添加到购物车</a> </td> </tr> <%}%> </table> </form> </body> <script> function addToCart(id) { var count = prompt('添加数量是多少个'); $("#id").val(id); $("#count").val(count); if (confirm("确认添加?")) { $("#form").submit(); } } </script> </html>
<%@ page import="model.Shop" %> <%@ page import="java.util.Map" %> <%@ page import="java.util.Set" %><%-- Created by IntelliJ IDEA. User: asus Date: 2019/1/10 Time: 23:40 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% Map<Map<Shop,Integer>,Integer> cart = (Map<Map<Shop, Integer>, Integer>) session.getAttribute("shopcart"); int sum = (int) session.getAttribute("sum"); %> <table> <tr> <th>水果名</th> <th>价格</th> <th>类别</th> <th>数量/个</th> <th>小计/元</th> </tr> <% Set<Map<Shop, Integer>> set = cart.keySet(); for (Map<Shop, Integer> shopIntegerMap : set) { Shop shop = shopIntegerMap.keySet().iterator().next(); %> <tr> <td><%=shop.getName()%></td> <td><%=shop.getPrice()%></td> <td><%=shop.getType()%></td> <td> <%=shopIntegerMap.get(shop)%></td> <td><%=cart.get(shopIntegerMap)%></td> </tr> <%}%> </table> <br> <th>合计:</th><%=sum%> 元 </body> </html>
出错代码待添加
//此处写的是Shop是因为数据表中给定的表名是shop package model; /** * TODO * * @Author Whyat * @Date 2019/1/9 17:10 */ public class Shop { private int sid,price; private String code,name,type; public Shop() { } public Shop(int price, String code, String name, String type) { this.price = price; this.code = code; this.name = name; this.type = type; } public Shop(int sid, int price, String code, String name, String type) { this.sid = sid; this.price = price; this.code = code; this.name = name; this.type = type; } public int getSid() { return sid; } public Shop setSid(int sid) { this.sid = sid; return this; } public int getPrice() { return price; } public Shop setPrice(int price) { this.price = price; return this; } public String getCode() { return code; } public Shop setCode(String code) { this.code = code; return this; } public String getName() { return name; } public Shop setName(String name) { this.name = name; return this; } public String getType() { return type; } public Shop setType(String type) { this.type = type; return this; } @Override public String toString() { return "Shop{" + "sid=" + sid + ", price=" + price + ", code='" + code + '\'' + ", name='" + name + '\'' + ", type='" + type + '\'' + '}'; } }
1 package servlet; 2 3 import model.Shop; 4 import service.ShopService; 5 import service.impl.ShopServiceImpl; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 import javax.servlet.http.HttpSession; 13 import java.io.IOException; 14 import java.util.HashMap; 15 import java.util.List; 16 import java.util.Map; 17 import java.util.Set; 18 19 /** 20 * TODO 21 * 22 * @Author Whyat 23 * @Date 2019/1/10 14:50 24 */ 25 @WebServlet("/shopcart") 26 public class ShopCart extends HttpServlet { 27 ShopService shopService = new ShopServiceImpl(); 28 29 @Override 30 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 31 int sid = Integer.parseInt(req.getParameter("goodid")); 32 int count = Integer.parseInt(req.getParameter("count")); 33 HttpSession session = req.getSession(); 34 session.setMaxInactiveInterval(60); 35 Shop shop = shopService.getShopBySid(sid); 36 boolean flag = true; 37 Integer sum = (Integer) session.getAttribute("sum"); 38 if (sum == null) { 39 sum = 0; 40 } 41 Map<Map<Shop, Integer>, Integer> cart = (Map<Map<Shop, Integer>, Integer>) session.getAttribute("shopcart"); 42 if (cart == null) { 43 cart = new HashMap<Map<Shop, Integer>, Integer>(); 44 } 45 Set<Map<Shop, Integer>> mapset = cart.keySet(); 46 for (Map<Shop, Integer> shopIntMap : mapset) { 47 Map<Shop,Integer> xmap = shopIntMap; 48 Set<Shop> shopSet = shopIntMap.keySet(); 49 Shop shopInSet = shopSet.iterator().next(); 50 if (shopInSet.getSid() == sid) { 51 //保存<商品,总价>的大map的总价 52 int oringinGoodSum = cart.get(shopIntMap); 53 //去掉<商品,总价>的大map里的<商品,数量>小map 54 cart.remove(shopIntMap); 55 //修改<商品,数量>小map的数量 56 shopIntMap.put(shopInSet, shopIntMap.get(shopInSet) + count); 57 //再将修改商品数量后的<商品,数量>小map添加到<商品,总价>的大map中 58 cart.put(shopIntMap, oringinGoodSum + shopInSet.getPrice() * count); 59 int singleSum = cart.get(shopIntMap); 60 int singleCount = shopIntMap.get(shopInSet); 61 sum += shop.getPrice() * count; 62 flag = false; 63 break; 64 } 65 } 66 if (flag) { 67 Map<Shop, Integer> newShopMap = new HashMap<>(); 68 newShopMap.put(shop, count); 69 cart.put(newShopMap, shop.getPrice() * count); 70 sum += shop.getPrice() * count; 71 } 72 session.setAttribute("shopcart", cart); 73 session.setAttribute("sum", sum); 74 req.getRequestDispatcher("cart.jsp").forward(req, resp); 75 76 } 77 78 @Override 79 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 80 //响应头部格式设置 81 resp.setCharacterEncoding("utf-8"); 82 resp.setContentType("text/plain"); 83 84 List<Shop> ul = new ShopServiceImpl().getAllShopInfo(); 85 req.setAttribute("shoplist", ul); 86 req.getRequestDispatcher("shopping.jsp").forward(req, resp); 87 } 88 }
结论
在Key嵌套HashMap的HashMap,如果修改已经存放的Key的内容时,再用修改后的外层Key去获取外层HashMap的Value,是会报空指针异常的。但是如果不是HashMap嵌套,这样做是不会出异常,以上结论仅根据做的简单的测试得出的,如有错误,望不吝赐教。
package servlet; import model.Good; import model.Shop; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * TODO * * @Author Whyat * @Date 2019/1/10 22:05 */ public class test { public static void main(String[] args) { //test1(); test2(); } /** * 修改map外面的key的内容内层的key的内容也会改变, * 再用修改后的key去获得之前的value是可以的, * 而且可以覆盖之前的键值对 */ private static void test1() { Map<Shop, Integer> map = new HashMap<>(); Shop shop = new Shop(2, 3, "code", "name", "type"); map.put(shop, 3); shop.setSid(4); int i = map.get(shop); Set<Shop> set = map.keySet(); for (Shop shop1 : set) { System.out.println(shop1); } map.put(shop, 4); System.out.println("..."); } /** * 修改外层的key,根据key获取外层的value会出现空指针异常 */ private static void test2() { Map<Map<Good,Integer>, Integer> bigMap = new HashMap<>(); Map<Good,Integer> smallMap = new HashMap<>(); Good good = new Good(1, "name", "class", 10, "code"); smallMap.put(good, 5); bigMap.put(smallMap, 100); //修改了内层小map的内容 smallMap.put(good, 6); //大map再根据小map获取之前大map的value报空指针异常 int i = bigMap.get(smallMap); bigMap.put(smallMap, i + 10); System.out.println("..."); } }