第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

Java 學(xué)校項目的地圖問題

Java 學(xué)校項目的地圖問題

狐的傳說 2024-01-25 15:13:04
我一直在做學(xué)校作業(yè)。目標(biāo):我正在提供一份超市顧客名單每個客戶都有一個郵政編碼和一個包含產(chǎn)品名稱的集合以及該客戶購買的這些產(chǎn)品的數(shù)量。我被要求返回一個地圖(Sting = zipCode,Product = Product),其中應(yīng)包含郵政編碼作為密鑰以及該郵政編碼最暢銷的產(chǎn)品。我得到的代碼:/** * (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;}我一直在嘗試在地圖中使用地圖,但在實現(xiàn)這一點時遇到問題。這還遠(yuǎn)未完成,根本無法編譯。/** * (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;}我可以采取哪些步驟來修復(fù)此實施?我只是尋求提示而不是完整的解決方案。
查看完整描述

2 回答

?
12345678_0001

TA貢獻1802條經(jīng)驗 獲得超5個贊

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


在 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);

    }

}

獲得計數(shù)最高的產(chǎn)品應(yīng)該相對簡單:


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);

}));

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


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

TA貢獻1804條經(jīng)驗 獲得超7個贊

我認(rèn)為您希望將 if 語句移至 for 循環(huán)的開頭,并且僅tmpMap在該郵政編碼尚不存在時才創(chuàng)建。如果它已經(jīng)存在,只需使用現(xiàn)有的并使用產(chǎn)品和數(shù)量更新它。


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);


    }


查看完整回答
反對 回復(fù) 2024-01-25
  • 2 回答
  • 0 關(guān)注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號