亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java 學校項目的地圖問題

Java 學校項目的地圖問題

狐的傳說 2024-01-25 15:13:04
我一直在做學校作業。目標:我正在提供一份超市顧客名單每個客戶都有一個郵政編碼和一個包含產品名稱的集合以及該客戶購買的這些產品的數量。我被要求返回一個地圖(Sting = zipCode,Product = Product),其中應包含郵政編碼作為密鑰以及該郵政編碼最暢銷的產品。我得到的代碼:/** * (DIFFICULT!!!) * calculates a map of most bought products per zip code that is also ordered by zip code * if multiple products have the same maximum count, just pick one. * @return */public Map<String, Product> mostBoughtProductByZipCode() {    Map<String, Product> mostBought = null;    // TODO create an appropriate data structure for the mostBought and calculate its contents    return mostBought;}我一直在嘗試在地圖中使用地圖,但在實現這一點時遇到問題。這還遠未完成,根本無法編譯。/** * (DIFFICULT!!!) * calculates a map of most bought products per zip code that is also ordered by zip code * if multiple products have the same maximum count, just pick one. * @return */public Map<String, Product> mostBoughtProductByZipCode() {    Map<String, Product> mostBought = null;    Map<String, Map<Product, Integer>> zipCodeProducts = new HashMap<>();    for (Customer customer : this.customers) {        String tmp = customer.getZipCode();        Map<Product, Integer> tmpMap = new HashMap<>();        for (Purchase purchase: customer.getItems()) {            tmpMap.put(purchase.getProduct(),purchase.getAmount());        }        if (!zipCodeProducts.containsKey(tmp)){            zipCodeProducts.put(tmp, tmpMap);        } else {            ???        }    }    // TODO create an appropriate data structure for the mostBought and calculate its contents    return mostBought;}我可以采取哪些步驟來修復此實施?我只是尋求提示而不是完整的解決方案。
查看完整描述

2 回答

?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

您的方向是正確的,但您需要考慮第一次找到郵政編碼/產品組合時會發生什么。


在 Java 的更高版本中,有很多Map方法可以使這變得更容易。我將在這里使用它們,但如果您必須使用早期版本,那么您將需要擴展其中一些語句。


像下面這樣:


Map<String, Map<Product, Integer>> zipCodeProducts = new HashMap<>();

for (Customer customer: customers) {

    Map<Product,Integer> productCounts = zipCodeProducts.computeIfAbsent(customer.getZipCode(), () -> new HashMap<>());

    for (Purchase purchase: customer.getItems()) {

        productCounts.merge(purchase.getProduct(), 1, Integer::sum);

    }

}

獲得計數最高的產品應該相對簡單:


Map<String,Integer> maxProducts = new HashMap<>();

zipCodeProducts.forEach((zc, pc) -> pc.forEach((pr, n) -> {

    if (!maxProducts.contains(zc) || n > pc.get(maxProducts.get(zc)))

        maxProducts.put(zc, pr);

}));

希望這是有道理的——如果沒有的話就問。


查看完整回答
反對 回復 2024-01-25
?
三國紛爭

TA貢獻1804條經驗 獲得超7個贊

我認為您希望將 if 語句移至 for 循環的開頭,并且僅tmpMap在該郵政編碼尚不存在時才創建。如果它已經存在,只需使用現有的并使用產品和數量更新它。


for (Customer customer : this.customers) {

        String tmp = customer.getZipCode();

        Map<Product, Integer> tmpMap;


        if (!zipCodeProducts.containsKey(tmp)){

             tmpMap = new HashMap<Product, Integer>();

        } else {

             tmpMap = zipCodeProducts.get(tmp);

        }



        for (Purchase purchase: customer.getItems()) {

            if (!tmpMap.containsKey(purchase.getProduct())) {

                tmpMap.put(purchase.getProduct(),purchase.getAmount());

            } else {

                tmpMap.put(purchase.getProduct(), tmpMap.get(purchase.getProduct()) + purchase.getAmount());

            }


        }


        zipCodeProducts.put(tmp, tmpMap);


    }


查看完整回答
反對 回復 2024-01-25
  • 2 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號