Java 如何在HashMap中添加所有值?

Java 如何在HashMap中添加所有值?,java,hashmap,Java,Hashmap,我有一个任务,我必须做一个杂货店股票价格的哈希图。然后,我要做一个方法,在这个方法中,一辆有这些物品的购物车存在,我要找到总价格 这是我的HashMap的一个片段,用于商店的商品和价格: HashMap<String, Double>stock= new HashMap<String, Double>(); stock.put("eggs",1.79); stock.put("orange juice",2.5); public static double pri

我有一个任务,我必须做一个杂货店股票价格的哈希图。然后,我要做一个方法,在这个方法中,一辆有这些物品的购物车存在,我要找到总价格

这是我的
HashMap
的一个片段,用于商店的商品和价格:

HashMap<String, Double>stock= new HashMap<String, Double>(); 
stock.put("eggs",1.79);
stock.put("orange juice",2.5); 


public static double price(HashMap<String, Double>stock){

    HashMap<String, Integer>cart = new HashMap<String, Integer>();
    cart.put("eggs", 2);
    cart.put("orange juice", 2);
}
HashMapstock=newhashmap();
库存量(鸡蛋),1.79;
股票卖出价(“橙汁”,2.5);
公共静态双倍价格(HashMapstock){
HashMapcart=新建HashMap();
推车放置(“鸡蛋”,2);
推车放置(“橙汁”,2);
}

这是我的购物车,其中int表示购物车中每种商品的数量。我对HashMaps非常陌生,对于如何将股票映射引用到price方法中并正确地将其相加,我感到非常困惑。理论上,最终的答案是两个鸡蛋和两盒橙汁的价格。非常感谢您的帮助

循环浏览购物车条目,将其转换为商品价格,然后求和

您可以为此使用流:

double total = cart.entrySet().stream()
    .mapToDouble(entry -> entry.getValue() * stock.get(entry.getKey()))
    .sum();

试试这个

public static double price(HashMap<String, Double> stock){


    HashMap<String, Integer> cart = new HashMap<String, Integer>();
    cart.put("eggs", 2);
    cart.put("orange juice", 2);
    // the cart hashmap now contains all the items in our shopping cart

    // we want to get the prices of all items in our cart, and multiple by the amount we are purchasing
    double cost = 0;
    for (String item: cart.keySet()) { // for every item we are purchasing
        double amount = cart.get(item); // how much we are purchasing
        double price = stock.get(item); // how much it costs
        cost += amount * price; // update the total price
    }
    return cost;
}
公共静态双倍价格(HashMap股票){
HashMap购物车=新建HashMap();
推车放置(“鸡蛋”,2);
推车放置(“橙汁”,2);
//购物车hashmap现在包含我们购物车中的所有项目
//我们希望得到我们购物车中所有物品的价格,并乘以我们购买的金额
双倍成本=0;
对于(字符串项:cart.keySet()){//对于我们正在购买的每个项
双倍金额=购物车。获取(物品);//我们购买了多少
双倍价格=库存。获取(物品);//成本
成本+=金额*价格;//更新总价
}
退货成本;
}

作为初学者,这段代码可能会有所帮助。这是一种精细/详细的做事方式:

import java.util.HashMap;
import java.util.Iterator;


public class Grocery {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         HashMap<String, Double>stock= new HashMap<String, Double>(); 
            stock.put("eggs",1.79);
            stock.put("orange juice",2.5); 

            HashMap<String, Integer>cart = new HashMap<String, Integer>();
            cart.put("eggs", 2);
            cart.put("orange juice", 2);
            System.out.println(price(stock,cart));//prints the total cart value/price

    }
            public static double price(HashMap<String, Double>stock,HashMap<String, Integer>cart){

              Iterator<String> productItr = cart.keySet().iterator();//get each product in the cart
              double price=0;
              while(productItr.hasNext()){
                  String product =productItr.next();
                  price=price+( cart.get(product)*stock.get(product));
              }


                return price;
        }

}
import java.util.HashMap;
导入java.util.Iterator;
公营杂货店{
/**
*@param args
*/
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
HashMapstock=新的HashMap();
库存量(鸡蛋),1.79;
股票卖出价(“橙汁”,2.5);
HashMapcart=新建HashMap();
推车放置(“鸡蛋”,2);
推车放置(“橙汁”,2);
System.out.println(price(stock,cart));//打印购物车的总价值/价格
}
公共静态双倍价格(HashMapstock、HashMapcart){
迭代器productItr=cart.keySet().Iterator();//获取购物车中的每个产品
双倍价格=0;
while(productItr.hasNext()){
字符串product=productItr.next();
价格=价格+(购物车获取(产品)*库存获取(产品));
}
退货价格;
}
}

上面的人已经给了你一些代码片段,但我将根据Chris Bertasi的回答更详细地说明发生了什么,因为这是最容易阅读的,因为你是Hashmaps新手

HashMap<String, Double>stock= new HashMap<String, Double>(); 
stock.put("eggs",1.79);
stock.put("orange juice",2.5); 
因此,要获得鸡蛋的价格,只需使用
stock.get(“egg”)
询问Hashmap,它就会返回值
1.79

同样的逻辑也适用于购物车,它的数量是数量,而不是价格(例如返回2而不是1.79)

因此,一旦我们将这些项目添加到购物车中,我们就要遍历它并获得总成本

要迭代我们的库存,我们可以使用:

for (String item: cart.keySet())
它将查看键集(即键列),每次获取一个键,并将其设置为项变量

利用这些知识,我们可以通过stock&cart获得每件商品的价格和用户购买的金额

double amount = cart.get(item); 
double price = stock.get(item); 
利用这些信息,我们可以通过以下方式生成总成本:

totalCost += amount * price;
把这些拼凑在一起,我们就得到了这个片段,在这里我们迭代每个关键元素,并通过
.get()
方法检索价格和数量

double totalCost = 0;
for (String item: cart.keySet()) {
    double amount = cart.get(item);
    double price = stock.get(item);
    totalCost += amount * price; 
}
return totalCost;

向我们展示您的尝试。请阅读。更重要的是:。
double totalCost = 0;
for (String item: cart.keySet()) {
    double amount = cart.get(item);
    double price = stock.get(item);
    totalCost += amount * price; 
}
return totalCost;