一张地图中的地图只会存储内部地图提供的最后一个条目。

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

A map of maps will put only the last entry provided to the inner map

问题

这是您的代码:

private static void addItem(String[] commandParsed, Set<Item> inventory, Map<String, Map<String, Item>> maps){
    Item item = new Item(commandParsed[1], Double.parseDouble(commandParsed[2]), commandParsed[3]);
    String mapName = commandParsed[3];
    Map<String, Item> map = new HashMap<>();
    if (inventory.contains(item)){
        System.out.printf("Error: Item %s already exists%n", item.name);
    }
    else {
        inventory.add(item);
        maps.put(mapName, map);
        map.put(item.name, item);
        System.out.printf("Ok: Item %s added successfully%n", item.name);
    }
}

示例输入:

add CowMilk 1.90 dairy
add BulgarianYogurt 1.90 dairy
add SmartWatch 1111.90 technology

您的库存是一个单独的集合,您也可以在那里正确添加物品。但是,我的方法按预期创建了"dairy"和"technology"的映射。然而,每个映射只包含一个条目,例如,CowMilk被BulgarianYogurt覆盖。

英文:

I am making Map&lt;String, Map&lt;String, Item&gt;&gt; maps = new HashMap&lt;&gt;(); this is my code :

  private static void addItem(String[] commandParsed, Set&lt;Item&gt; inventory, Map&lt;String, Map&lt;String, Item&gt;&gt; maps){
            Item item = new Item(commandParsed[1], Double.parseDouble(commandParsed[2]), commandParsed[3]);
            String mapName = commandParsed[3];
            Map&lt;String, Item&gt; map = new HashMap&lt;&gt;();
            if (inventory.contains(item)){
                System.out.printf(&quot;Error: Item %s already exists%n&quot;, item.name);
            }
            else {
                inventory.add(item);
                maps.put(mapName, map);
                map.put(item.name, item);
                System.out.printf(&quot;Ok: Item %s added successfully%n&quot;, item.name);
            }
        }

Sample input:

add CowMilk 1.90 dairy
add BulgarianYogurt 1.90 dairy
add SmartWatch 1111.90 technology

inventory is a separate set, to which I also add the items, but it adds them there correctly. My method creates the map dairy and map technology as expected. However each map contains only one entry, for example CowMilk is overwritten by BulgarianYogurt

答案1

得分: 1

在这一行中,您每次都会覆盖地图:maps.put(mapName, map);
只有在第一次输入时才放入空地图:maps.computeIfAbsent(mapName,key -> new HashMap<>());
然后添加条目:maps.get(mapName).put(item.name,item)

英文:

You overwrite map each time in this line: maps.put(mapName, map);;
Put an empty map if it's the first entry only: maps.computeIfAbsent(mapName, key -&gt; new HashMap&lt;&gt;());.
Then add the entry: maps.get(mapName).put(item.name, item).

答案2

得分: 1

这只需做以下操作:

  • 如果键不存在,这将添加哈希映射。
  • 无论哪种情况,它都会返回新的Map或旧的Map,允许您输入键/值对。
maps.computeIfAbsent(mapName, k -> new HashMap<>()).put(item.name, item);

另外,似乎没有必要维护已添加项的列表。为什么不只检查当前的Map呢?将其放在一起,您可以这样做。

private static void addItem(String[] commandParsed, Map<String, Map<String, Item>> maps){
    Item item = new Item(commandParsed[1],
                         Double.parseDouble(commandParsed[2]),
                         commandParsed[3]);
    String mapName = commandParsed[3];
    if (maps.containsKey(mapName) && maps.get(mapName).containsKey(item.name)) {
        System.out.printf("Error: Item %s already exists%n", item.name);
    }  else {
          maps.computeIfAbsent(mapName, k -> new HashMap<>()).put(item.name, item);
    }	    
}

如果我理解您的意图有误,请告诉我。

英文:

All you need to do is the following:

  • This will put add the hashMap for the key if it doesn't exist.
  • In either case, it returns the new Map or the old one allowing you to
    enter the key/value pair.
maps.computeIfAbsent(mapName, k-&gt;new HashMap&lt;&gt;()).put(item.name, item);

Also, it does not seem to be necessary to maintain a list of already added items. Why not just check the current map. Putting it altogether you could do.

private static void addItem(String[] commandParsed, Map&lt;String, Map&lt;String, Item&gt;&gt; maps){
    Item item = new Item(commandParsed[1],
                         Double.parseDouble(commandParsed[2]),
                         commandParsed[3]);
    String mapName = commandParsed[3];
    if (maps.containsKey(mapName) &amp;&amp; maps.get(mapName).containsKey(item.name)) {
  	    System.out.printf(&quot;Error: Item %s already exists%n&quot;, item.name);
    }  else {
  	      maps.computeIfAbsent(mapName, k-&gt;new HashMap&lt;&gt;()).put(item.name,item);
    }	    
}

Please let me know if I misunderstood your intent.

huangapple
  • 本文由 发表于 2020年8月10日 00:11:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63328607.html
匿名

发表评论

匿名网友

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

确定