HashMap获取到了意外的字符串作为键值。

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

HashMap getting unintended string for key value

问题

以下是翻译好的代码部分:

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);
        System.out.printf("Ok: Item %s added successfully%n", item.name);
        maps.computeIfAbsent(mapName, k -> new HashMap<>()).put(item.name, item);
    }
}

这部分代码的目的是将唯一的物品添加到一个集合(inventory),然后创建一个 Map<String, Map<String, Item>> maps,其中键是添加的物品类型,值是包含该类型所有物品的映射,但内部映射的键仍然是物品类型而不是物品名称。

以下是您提供的示例输入:

add CowMilk 1.90 dairy
add BulgarianYogurt 1.90 dairy

您尝试找出为什么内部映射的键值对不是 <item.name, Item> 而是 <item.type, Item>,因为您的代码中使用的是 .put(item.name, item);

这是您的Item类构造函数:

public Item(String name, double price, String type) {
    this.name = name;
    this.price = price;
    this.type = type;
}

希望这有助于解决您的问题。

英文:

I have this piece of 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);
            System.out.printf(&quot;Ok: Item %s added successfully%n&quot;, item.name);
             maps.computeIfAbsent(mapName, k -&gt; new HashMap&lt;&gt;()).put(item.name, item);
        }
    }

my Idea is to add all unique items to a set and then to make Map&lt;String, Map&lt;String, Item&gt;&gt; maps where the key is the type of the added item and the value to be map containing all items of that type, but the key to be the Item names. However the keys of the inner maps are again the type of the item. here is some sample input

add CowMilk 1.90 dairy
add BulgarianYogurt 1.90 dairy

I am trying to find out why my inner map key-value pair is not &lt;item.name, Item&gt; but &lt;item.type, Item&gt; since my code is .put(item.name, item);

This is my Item class constructor

 public Item(String name, double price, String type) {
        this.name = name;
        this.price = price;
        this.type = type;
    }

HashMap获取到了意外的字符串作为键值。

答案1

得分: 2

你误读了调试器对象树的显示。

映射条目是键值对

"dairy" 映射条目是一个键值对,键是 "dairy",值是另一个映射(具有键 "CowMilk""BulgarianYogurt")。

因此,并不是内部映射具有 "dairy" 键,而是展开 "dairy" 映射条目会将 "dairy" 暴露为映射条目中的。内部映射的键是 "CowMilk""BulgarianYogurt"

英文:

You are misreading the debugger object tree display.

Map entries are key-value pairs.

The &quot;dairy&quot; map entry is a key-value pair with key "dairy", and a value that is another map (with keys "CowMilk" and "BulgarianYogurt")

So it's not that the inner map has a "dairy" key, it's just that expanding the "dairy" map entry exposes "dairy" as the key in the map entry. The keys of the inner map are "CowMilk" and "BulgarianYogurt".

答案2

得分: 1

你需要将 {item.name, item} 放入内部映射中,如下所示:

maps.computeIfAbsent(mapName, k -> new HashMap<String, Item>().put(item.name, item));
英文:

> I am trying to find out why my inner map key-value pair is not
> <item.name, Item> but <item.type, Item> since my code is
> .put(item.name, item);

You need to put {item.name, item} inside the inner map as shown below:

maps.computeIfAbsent(mapName, k -&gt; new HashMap&lt;String, Item&gt;().put(item.name, item));

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

发表评论

匿名网友

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

确定