Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.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按键减去HashMaps中的值_Java_Collections_Hashmap_Subtraction - Fatal编程技术网

Java按键减去HashMaps中的值

Java按键减去HashMaps中的值,java,collections,hashmap,subtraction,Java,Collections,Hashmap,Subtraction,我需要一些帮助,我正在自学如何用Java处理映射,今天我试图通过键从Hashmap中减去值,但现在我被卡住了 这些是我想要减去的贴图值(带键的值之间的算术减法) 您必须首先将这些字符串解析为整数。这是因为您将值存储为String,无法对其执行算术运算 Integer.parseInt(map.get(1)) - Integer.parseInt(map.get(2)) 或者,您可以像这样将值存储为整数 HashMap<Integer,Integer> map = new HashM

我需要一些帮助,我正在自学如何用Java处理映射,今天我试图通过键从Hashmap中减去值,但现在我被卡住了

这些是我想要减去的贴图值(带键的值之间的算术减法)


您必须首先将这些字符串解析为整数。这是因为您将值存储为
String
,无法对其执行算术运算

Integer.parseInt(map.get(1)) - Integer.parseInt(map.get(2))
或者,您可以像这样将值存储为整数

HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
map.put(1, 10);
map.put(2, 5);

您必须首先将这些字符串解析为整数。这是因为您将值存储为
String
,无法对其执行算术运算

Integer.parseInt(map.get(1)) - Integer.parseInt(map.get(2))
或者,您可以像这样将值存储为整数

HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
map.put(1, 10);
map.put(2, 5);

您需要首先将字符串解析为整数,然后对其进行减法运算

它是这样的:-

int result = Integer.parseInt(map.get(1)) - Integer.parseInt(map.get(2))

您需要首先将字符串解析为整数,然后对其进行减法运算

它是这样的:-

int result = Integer.parseInt(map.get(1)) - Integer.parseInt(map.get(2))

如果您想对这些值进行算术运算,为什么要将它们存储为字符串?好的,我会更正这个问题。您应该使用
Map
。然后问题就会消失。如果你想对这些值进行算术运算,为什么要将它们存储为字符串?好的,我会纠正这个问题。你应该使用
Map
。然后问题就会消失。