Java集合中HashMap日常问题及解决办法
2019獨角獸企業重金招聘Python工程師標準>>>
前言
今天在學習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)" οnclick="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是因為數據表中給定的表名是shoppackage 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 + '\'' +
??????????????? '}';
??? }
}
?
結論
?
在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("...");
??? }
}
轉載于:https://my.oschina.net/u/3980693/blog/3000011
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Java集合中HashMap日常问题及解决办法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode: Median of
- 下一篇: PostgreSQL JDBC SQLW