學習中遇到的HashMap問題

来源:https://www.cnblogs.com/whyat/archive/2019/01/11/10253302.html
-Advertisement-
Play Games

前言 今天在老師教Session的時候,利用了Session可持久化保存伺服器端的特性嘗試做了一下用HashMap嵌套的購物車(沒有將購物車的商品信息保存在資料庫中),之所以做的這麼麻煩是為了鞏固之前學習的Map知識和鍛煉邏輯能力,正好也在其中遇到了一個關於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>
商品選擇jsp
<%@ 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>&nbsp;&nbsp;<%=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 + '\'' +
                '}';
    }
}
商品Bean
 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 }
修改後的Servlet

 結論

在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("...");
    }
}
測試

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 橋接適用於把抽象化和實現化解耦,使得二者可以獨立變化。這種類型的設計模式屬於結構性模式,它通過提供抽象化和實現化之間的橋接結構,來實現二者的解耦 ...
  • 單例模式是軟體工程中最著名的模式之一。從本質上講,單例是一個只允許創建自身的單個實例的類,並且通常可以簡單地訪問該實例。最常見的是,單例不允許在創建實例時指定任何參數——否則對實例的第二個請求但具有不同的參數可能會有問題!(如果對於具有相同參數的所有請求都應訪問相同的實例,則工廠模式更合適。)本文... ...
  • 上篇已提(tu)到(cao)Java中的各種坑。習慣了C 的各種特性和語法糖後,再轉到Java感覺比較彆扭。最後本著反正Java也不是很熟悉,乾脆再折騰折騰其他語言的破罐子破摔的心態,逛了一圈JVM語言,最終決定轉Kotlin。 為何選擇Kotlin 項目遭遇人員變動,包括我在內就剩兩個人開發,轉型 ...
  • 異常處理機制在一定程度上保證了程式的健壯性,就好像給程式罩了一層保險。 ...
  • Python基礎部分 Python基礎 Python基礎 編碼 小數據池&is與==區別 深淺拷貝 文件操作 Python基礎 編碼 小數據池&is與==區別 深淺拷貝 文件操作 數據結構 sting tuple list dictionary set 推導式 sting tuple list di ...
  • PS:教科書般的文章太多了,我要追求與眾不同,註意是追求。授人以魚不如授人以漁。 相關文章 如何慢慢地快速成長起來? 你是如何看待Spring容器的,是這樣子嗎? 👉§認真思考,才能理解深刻 我們經常會說我在開發中用到了Spring,這句話沒問題。但仔細思考下,也可以說成我寫的代碼用到了Sprin ...
  • 作為一個活躍在京津冀地區的開發者,要閑著沒事就看看`石家莊`這個國際化大都市的一些數據,這篇博客爬取了鏈家網的租房信息,爬取到的數據在後面的博客中可以作為一些數據分析的素材。 我們需要爬取的網址為:`https://sjz.lianjia.com/zufang/` ...
  • 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=6441(本題來源於2018年中國大學生程式設計競賽網路選拔賽) 題意:輸入n和a,求滿足等式a^n+b^n=c^n的b,c的值 思路: 首先我們要知道什麼是費馬大定理 百度詞條 費馬大定理,又被稱為“費馬 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...