【问题标题】:How to return key and it's values if the string matches a key entry如果字符串与键条目匹配,如何返回键及其值
【发布时间】:2019-05-08 17:57:06
【问题描述】:

所以我创建了一个类,它创建一个键映射,这些键是菜肴名称的字符串,每个键都有一组字符串值,它们是菜肴中的成分。我已经设法打印出所有的键值对,但现在我想要一个将字符串作为参数的方法,然后如果该字符串与键匹配,则打印出键值对,如果不匹配,则显示一条消息说没有找到这样的密钥。

这是我的尝试:

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");
    }
}

到目前为止,这也是我的完整类代码,在 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");
        }
    }
}

【问题讨论】:

标签: java hashmap key-value


【解决方案1】:

keySet 不带任何参数。该方法返回整个键集,在这种情况下

[Risotto, Burger, Pizza]

您想要查找 get 方法。

System.out.println("The ingredients for " + a + " Are: " + recipes.get(a));

【讨论】:

    【解决方案2】:

    检查以下代码,

    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");
        }
    }
    

    【讨论】:

      【解决方案3】:

      java 1.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"));
          }
      

      【讨论】:

        猜你喜欢
        • 2017-03-22
        • 2021-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多