Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将一个整数映射到一个对象的最佳方式是什么?将对象作为键_Java_Hashmap_Key_Mapping - Fatal编程技术网

Java 将一个整数映射到一个对象的最佳方式是什么?将对象作为键

Java 将一个整数映射到一个对象的最佳方式是什么?将对象作为键,java,hashmap,key,mapping,Java,Hashmap,Key,Mapping,嘿,这可能是个愚蠢的问题,但尽管我可以使用put函数将对象映射到整数: product Tuna = new product(1, nutrientsIn); product Milk = new product(0, nutrientsIn2); HashMap<product, Integer> productQuantity = new HashMap<product, Integer>(); productQuantity.put(Tuna, 2); pr

嘿,这可能是个愚蠢的问题,但尽管我可以使用put函数将对象映射到整数:

product Tuna = new product(1, nutrientsIn);

product Milk = new product(0, nutrientsIn2);

HashMap<product, Integer> productQuantity = new HashMap<product, Integer>();

productQuantity.put(Tuna, 2);

productQuantity.put(Milk, 4);

Diet.totalNutrients(productQuantity);

我得到一个错误:找不到符号。我想这意味着它在寻找牛奶变量

这是正确的方法吗?如果是的话,我该怎么做?或者有更好的方法吗

  • 错误:找不到符号

    • 您之所以得到这个结果,是因为您使用的变量MILK超出了它的范围
  • 备用方式

    • 您可以为产品创建枚举

  • 当前方法的代码

    public class Sample {
        public static void main(String[] args) {
            Product Tuna = new Product(1, "nutrientsIn");
    
            Product Milk = new Product(0, "nutrientsIn2");
    
            HashMap<Product, Integer> productQuantity = new HashMap<Product, Integer>();
    
            productQuantity.put(Tuna, 2);
    
            productQuantity.put(Milk, 4);
    
    //        Diet.totalNutrients(productQuantity);
    
            // Use this if in same block
            System.out.printf("%d\n", productQuantity.get(Milk));
            // Use this if in some other block (where getting the error)
            Product makeMilkObject = new Product(0, "nutrientsIn2");
            System.out.printf("%d\n", productQuantity.get(makeMilkObject));
        }
    }
    
    class Product{
        int key;
        String nutrient;
        Product(int key, String nutrient){
            this.key = key;
            this.nutrient = nutrient;
        }
    
        public int getKey() {
            return key;
        }
    
        public String getNutrient() {
            return nutrient;
        }
    
        @Override
        public boolean equals(Object obj) {
            return (this.key == ((Product)obj).getKey()) && (this.getNutrient().equals(((Product) obj).getNutrient()));
        }
    
        @Override
        public int hashCode() {
            return this.getKey();
        }
    }
    
    公共类示例{
    公共静态void main(字符串[]args){
    产品=新产品(1,“营养素”);
    产品乳=新产品(0,“营养素2”);
    HashMap productQuantity=新HashMap();
    产品数量。放置(金枪鱼,2);
    产品数量。放置(牛奶,4);
    //饮食。总营养素(产品数量);
    //如果在同一块中,请使用此选项
    System.out.printf(“%d\n”,productQuantity.get(Milk));
    //如果在其他块中(获取错误的位置),请使用此选项
    Product makeMilkObject=新产品(0,“营养素2”);
    System.out.printf(“%d\n”,productQuantity.get(makeMilkObject));
    }
    }
    类产品{
    int键;
    营养素;
    产品(int键、字符串){
    this.key=key;
    这个。营养素=营养素;
    }
    public int getKey(){
    返回键;
    }
    公共字符串{
    归还养分;
    }
    @凌驾
    公共布尔等于(对象obj){
    return(this.key==((产品)obj.getKey())和&(this.get营养品().equals)((产品)obj.get营养品());
    }
    @凌驾
    公共int hashCode(){
    返回这个.getKey();
    }
    }
    
    您在
    产品
    类中实现了
    等于
    吗?我没有。我现在试过了,但它找不到。您能进一步解释一下您的意思吗?通常在Java中,对象strt以小写字母(tuna)开头,类以大写字母(tuna)开头。我可能有点误解,但我试图使用product对象作为映射中的键,并且值是整数。我不希望quanity变量存储为类变量。@ras64确定。。所以我正在编辑我的答案。我忽略了这一点。我假设您正在将productKeyArgument存储为map中的值。@ras64您也可以使用枚举,因此,当值来自用作键的类时,我可以这样做。然而,我不确定是否有可能将一个单独的原语作为一个值。如果是这样的话,你能推荐其他达到类似结果的方法吗?
    public class Sample {
        public static void main(String[] args) {
            Product Tuna = new Product(1, "nutrientsIn");
    
            Product Milk = new Product(0, "nutrientsIn2");
    
            HashMap<Product, Integer> productQuantity = new HashMap<Product, Integer>();
    
            productQuantity.put(Tuna, 2);
    
            productQuantity.put(Milk, 4);
    
    //        Diet.totalNutrients(productQuantity);
    
            // Use this if in same block
            System.out.printf("%d\n", productQuantity.get(Milk));
            // Use this if in some other block (where getting the error)
            Product makeMilkObject = new Product(0, "nutrientsIn2");
            System.out.printf("%d\n", productQuantity.get(makeMilkObject));
        }
    }
    
    class Product{
        int key;
        String nutrient;
        Product(int key, String nutrient){
            this.key = key;
            this.nutrient = nutrient;
        }
    
        public int getKey() {
            return key;
        }
    
        public String getNutrient() {
            return nutrient;
        }
    
        @Override
        public boolean equals(Object obj) {
            return (this.key == ((Product)obj).getKey()) && (this.getNutrient().equals(((Product) obj).getNutrient()));
        }
    
        @Override
        public int hashCode() {
            return this.getKey();
        }
    }