为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 35 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信