英文:
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<String, Map<String, Item>> maps = new HashMap<>();
this is my 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);
maps.put(mapName, map);
map.put(item.name, item);
System.out.printf("Ok: Item %s added successfully%n", 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 -> new HashMap<>());
.
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->new HashMap<>()).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<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);
}
}
Please let me know if I misunderstood your intent.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论