使用一个字符串变量作为HashMap的名称。

huangapple go评论101阅读模式
英文:

using a String variable for a name of a HashMap

问题

我正在使用一个while循环来读取一组命令。命令的格式如下:添加 项目 价格 类型。
我将类型放入一个字符串集合中,然后如果该类型不存在,我想要创建一个新的哈希映射,其中包含项目(字符串)和价格(双精度)。映射的名称应该是类型,但它不允许我使用字符串变量来命名。有没有办法绕过这个问题?

while (!input.equals("end")){
    String[] command = input.split(" ");
    if (!categories.contains(command[3])){
        categories.add(command[3]);
        String mapName = command[3];
        Map<String, Double> map = new HashMap<>();
    }
}

我需要映射的名称例如可以是"shampoos"、"food"或者输入的任何内容,而不是mapName。

英文:

I am using a while loop to read a set of commands. the command is in the following format : add item price type.
I place the types in a set of Strings, after which if the type does not exist I want to create a new hashmap containing the item(String) and the price(Double). the name of the map should be the type, but it does not allow me to use a String variable for it. Is there a way to get around that?

 while (!input.equals(&quot;end&quot;)){
            String[] command = input.split(&quot; &quot;);
            if (!categories.contains(command[3])){
                categories.add(command[3]);
                String mapName = command[3];
                Map&lt;String, Double&gt;  mapName = new HashMap&lt;&gt;();
            }
        }

I need the name of the map the be for example shampoos or food or whatever the input was, not mapName.

答案1

得分: 2

你需要一个包含多个映射的映射表,外部映射的键是内部映射的名称,例如:

Map<String, Map<String, Double>> maps = new HashMap<>();

在输入不等于 "end" 的情况下,可以执行以下操作:

String[] command = input.split(" ");
if (!categories.contains(command[3])){
    categories.add(command[3]);
    String mapName = command[3];
    Map<String, Double> map = new HashMap<>();
    maps.put(mapName, map);
}

然后,你可以通过它们的名称来获取这些映射,例如:

maps.get(<映射名称>)

请注意,上述代码示例中的 "<映射名称>" 应该替换为实际的映射名称。

英文:

You will need a map of maps, where the keys of the outter map are the names of the inner maps, like:

Map&lt;String, Map&lt;String, Double&gt;&gt; maps = new HashMap&lt;&gt;();

while (!input.equals(&quot;end&quot;)){
    String[] command = input.split(&quot; &quot;);
    if (!categories.contains(command[3])){
        categories.add(command[3]);
        String mapName = command[3];
        Map&lt;String, Double&gt;  map = new HashMap&lt;&gt;();
        maps.put(mapName, map);
    }
}

then you can later get the maps by using their name like:

maps.get(&lt;map name&gt;)

答案2

得分: 1

不,你不能在Java中动态创建变量。

你需要创建一个嵌套的映射:

Map<String, Map<String, Double>> maps = new HashMap<>();
maps.put(mapName, new HashMap<>());

在你的程序中,使用映射来存储数据时需要非常小心。映射不是类的好替代品。

唯一的情况是,如果你真的不知道将输入什么值,才应该使用映射,而不是定义一个具有固定字段名称的类。如果输入值是已知的,几乎总是更好地定义类并将输入解析为类字段。

英文:

No, you can't create variables dynamically in Java.

You will need to create a map of maps:

Map&lt;String,Map&lt;String,Double&gt;&gt; maps = new HashMap&lt;&gt;():

maps.put(mapName, new HashMap&lt;&gt;());

I would be very careful with using maps to store data in your program. Maps are a very bad substitute for classes.

The only case where you should use a map instead of defining a class with fixed field names is if you really don't know the values that will be input. If the input values are known it is almost always better to define classes and parse the input into the class fields.

huangapple
  • 本文由 发表于 2020年7月30日 14:26:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63167345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定