如果字符串与键条目匹配,如何 return 键及其值
How to return key and it's values if the string matches a key entry
所以我创建了一个 class 来创建一个键映射,这些键是菜肴名称的字符串,每个键都有一组字符串值,这些字符串值是菜肴中的成分。我已经设法打印所有键值对,但现在我想要一个方法,该方法将字符串作为参数,然后如果该字符串与键匹配,则打印出键值对,如果不匹配,则显示一条消息说没有找到这样的密钥。
这是我的尝试:
public void printMapValue(String a) {
if (recipes.containsKey(a)) {
System.out.println("The ingredients for " + a + " Are: " + ingredients);
} else {
System.out.println("That string does not match a record");
}
}
到目前为止,这也是我的完整 class 代码,在 printMapVale()
方法
之前,它们都按预期工作
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.keySet(a));
} else {
System.out.println("That string does not match a record");
}
}
}
keySet
不接受任何参数。该方法 returns 整组键,在本例中为
[Risotto, Burger, Pizza]
你想做一个look-up,也就是get
方法。
System.out.println("The ingredients for " + a + " Are: " + recipes.get(a));
检查以下代码,
public void printMapValue(String a) {
Set<String> resultSet = null;
for (Map.Entry<String, Set<String>> entry : recipes.entrySet()) {
if (entry.getKey().equals(a)) {
resultSet = entry.getValue();
System.out.println("The ingredients for " + a + " Are: " + resultSet);
}
}
if (Objects.isNull(resultSet)) {
System.out.println("That string does not match a record");
}
}
有了java1.9+,就可以写成
public void printMapValue(String a) {
recipes.entrySet().stream()
.filter(e -> a.equals(e.getKey()))
.findFirst().ifPresentOrElse(e -> System.out
.println("The ingredients for " + e.getKey() + " Are: " + e.getValue(),
System.out.println("That string does not match a record"));
}
所以我创建了一个 class 来创建一个键映射,这些键是菜肴名称的字符串,每个键都有一组字符串值,这些字符串值是菜肴中的成分。我已经设法打印所有键值对,但现在我想要一个方法,该方法将字符串作为参数,然后如果该字符串与键匹配,则打印出键值对,如果不匹配,则显示一条消息说没有找到这样的密钥。
这是我的尝试:
public void printMapValue(String a) {
if (recipes.containsKey(a)) {
System.out.println("The ingredients for " + a + " Are: " + ingredients);
} else {
System.out.println("That string does not match a record");
}
}
到目前为止,这也是我的完整 class 代码,在 printMapVale()
方法
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.keySet(a));
} else {
System.out.println("That string does not match a record");
}
}
}
keySet
不接受任何参数。该方法 returns 整组键,在本例中为
[Risotto, Burger, Pizza]
你想做一个look-up,也就是get
方法。
System.out.println("The ingredients for " + a + " Are: " + recipes.get(a));
检查以下代码,
public void printMapValue(String a) {
Set<String> resultSet = null;
for (Map.Entry<String, Set<String>> entry : recipes.entrySet()) {
if (entry.getKey().equals(a)) {
resultSet = entry.getValue();
System.out.println("The ingredients for " + a + " Are: " + resultSet);
}
}
if (Objects.isNull(resultSet)) {
System.out.println("That string does not match a record");
}
}
有了java1.9+,就可以写成
public void printMapValue(String a) {
recipes.entrySet().stream()
.filter(e -> a.equals(e.getKey()))
.findFirst().ifPresentOrElse(e -> System.out
.println("The ingredients for " + e.getKey() + " Are: " + e.getValue(),
System.out.println("That string does not match a record"));
}