Итак, у меня есть работающий хэш-набор, но мне нужно добавить метод, который принимает два аргумента (строка ключа и строка набора), который проверяет, существует ли ключ в HashSet, и, если он есть, перезаписывает его новыми значениями набора иесли он не создаст его и не добавит в него предоставленные значения
код, который я уже пробовал:
public void addMapEntry(String dish, Set<String> ingredient){
recipes.get(dish);
if (recipes.containsKey(dish)) {
recipes.replace(dish, ingredient);
} else {
recipes.put(dish, ingredient);
}
}
Код для полного класса:
public class Recipe {
Map<String, Set<String>> recipes;
public Recipe() {
this.recipes = new HashMap<>();
}
public void addData() {
Set<String> ingredients = new HashSet<>();
ingredients.add("Rice");
ingredients.add("Stock");
recipes.put("Risotto", ingredients);
ingredients = new HashSet<>();
ingredients.add("Bun");
ingredients.add("Patty");
ingredients.add("Cheese");
ingredients.add("Lettuce");
recipes.put("Burger", ingredients);
ingredients = new HashSet<>();
ingredients.add("Base");
ingredients.add("Sauce");
ingredients.add("Cheese");
ingredients.add("Pepperoni");
recipes.put("Pizza", ingredients);
}
public void printMap() {
for(String recipeKey : recipes.keySet()) {
System.out.print("Dish : " + String.valueOf(recipeKey) + " Ingredients:");
for (String dish : recipes.get(recipeKey)) {
System.out.print(" " + dish + " ");
}
System.out.println();
}
}
public void printMapValue(String a) {
if (recipes.containsKey(a)) {
System.out.println("The ingredients for " + a + " Are: " + recipes.get(a));
} else {
System.out.println("That string does not match a record");
}
}
public void addMapEntry(String dish, Set<String> ingredient){
recipes.get(dish);
if (recipes.containsKey(dish)) {
recipes.replace(dish, ingredient);
} else {
recipes.put(dish, ingredient);
}
}}
Я получаю сообщение об ошибке: не удается найти символ - метод replace (java.lang.String, java.util.Set)
, что, я думаю, означает, что мне нужно где-то добавить toString ()!