Java 学校项目的地图问题

我一直在做学校作业。

目标:

  • 我正在提供一份超市顾客名单

  • 每个客户都有一个邮政编码和一个包含产品名称的集合以及该客户购买的这些产品的数量。

  • 我被要求返回一个地图(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;

}

我可以采取哪些步骤来修复此实施?我只是寻求提示而不是完整的解决方案。


狐的传说
浏览 91回答 2
2回答

12345678_0001

您的方向是正确的,但您需要考虑第一次找到邮政编码/产品组合时会发生什么。在 Java 的更高版本中,有很多Map方法可以使这变得更容易。我将在这里使用它们,但如果您必须使用早期版本,那么您将需要扩展其中一些语句。像下面这样:Map<String, Map<Product, Integer>> zipCodeProducts = new HashMap<>();for (Customer customer: customers) {&nbsp; &nbsp; Map<Product,Integer> productCounts = zipCodeProducts.computeIfAbsent(customer.getZipCode(), () -> new HashMap<>());&nbsp; &nbsp; for (Purchase purchase: customer.getItems()) {&nbsp; &nbsp; &nbsp; &nbsp; productCounts.merge(purchase.getProduct(), 1, Integer::sum);&nbsp; &nbsp; }}获得计数最高的产品应该相对简单:Map<String,Integer> maxProducts = new HashMap<>();zipCodeProducts.forEach((zc, pc) -> pc.forEach((pr, n) -> {&nbsp; &nbsp; if (!maxProducts.contains(zc) || n > pc.get(maxProducts.get(zc)))&nbsp; &nbsp; &nbsp; &nbsp; maxProducts.put(zc, pr);}));希望这是有道理的——如果没有的话就问。

三国纷争

我认为您希望将 if 语句移至 for 循环的开头,并且仅tmpMap在该邮政编码尚不存在时才创建。如果它已经存在,只需使用现有的并使用产品和数量更新它。for (Customer customer : this.customers) {&nbsp; &nbsp; &nbsp; &nbsp; String tmp = customer.getZipCode();&nbsp; &nbsp; &nbsp; &nbsp; Map<Product, Integer> tmpMap;&nbsp; &nbsp; &nbsp; &nbsp; if (!zipCodeProducts.containsKey(tmp)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tmpMap = new HashMap<Product, Integer>();&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tmpMap = zipCodeProducts.get(tmp);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for (Purchase purchase: customer.getItems()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!tmpMap.containsKey(purchase.getProduct())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpMap.put(purchase.getProduct(),purchase.getAmount());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpMap.put(purchase.getProduct(), tmpMap.get(purchase.getProduct()) + purchase.getAmount());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; zipCodeProducts.put(tmp, tmpMap);&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java