英文:
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<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);
}
}
my Idea is to add all unique items to a set and then to make Map<String, Map<String, Item>> 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 <item.name, Item>
but <item.type, Item>
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;
}
答案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 "dairy"
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 -> new HashMap<String, Item>().put(item.name, item));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论