简体   繁体   English

检查一个String数组是否包含另一个String数组的所有值(Java)

[英]Checking to see if a String array contains all of the values of another String array (Java)

I am a novice coder trying to teach myself how to code. 我是一个新手编码人员,试图教自己如何编码。 I am trying to create a program that stores a list of recipes with their corresponding ingredients, and then will suggest recipes based on the ingredients entered by the user. 我正在尝试创建一个程序,该程序存储配方及其相应成分的列表,然后根据用户输入的成分来建议配方。 I am using a HashMap with String keys (for the recipe name) and a String[] to represent the corresponding ingredients. 我正在使用带有字符串键(用于配方名称)和String []的HashMap来表示相应的成分。

The problem I have is that when a user enters the ingredients (split by commas), I can't seem to use the resulting values to check whether or not those values are contained in the corresponding value of the HashMap. 我的问题是,当用户输入成分(用逗号分隔)时,我似乎无法使用结果值来检查这些值是否包含在HashMap的相应值中。

When I try to call my ingredientSearch() method, the program returns the exception: "Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String at RecipeBox.ingredientSearch(RecipeBox.java:55)." 当我尝试调用我的IngredientSearch()方法时,程序返回异常:“线程“ main”中的异常” java.lang.ClassCastException:[Ljava.lang.String;无法在RecipeBox.ingredientSearch上转换为java.lang.String。 (RecipeBox.java:55)。”

Why doesn't this work, and how can I fix this? 为什么这行不通,我该如何解决?

import java.util.*;
import java.util.Map.Entry;

public class RecipeBox {

private String recipe;
private String name;
private String userInput;
private String randomRecipe;


Scanner input = new Scanner(System.in);
HashMap<String, String[]> recipes = new HashMap<String, String[]>();

public void addRecipe() {
    System.out.println("What is the name of your recipe?");
    name = input.nextLine();

    System.out.println("Enter the ingredients for " + name + " separated by commas:");
    recipe = input.nextLine();
    String[] ingredientList = recipe.split(",");

    recipes.put(name, ingredientList);

}

public void ingredientSearch() {

    System.out.println("What ingredients do you have?  Please enter your ingredients, separated by commas.");
    userInput = input.nextLine();
    String[] ingredientList = userInput.split(",");
    String check = ingredientList.toString();

    Iterator<Entry<String, String[]>> entries = recipes.entrySet().iterator();

    while (entries.hasNext()) {
        Entry entry = entries.next();
        String key = (String) entry.getKey();
        String value = (String) entry.getValue();
        if (value.contains(check)) {
            System.out.println("You could make " + key);
        }
    }
}
String value = (String) entry.getValue();

should be 应该

String[] value = entry.getValue();

Your value is a String array. 您的值是一个String数组。

First off, you're using a raw type, Entry , when it should be Entry<String, String[]> , as that will prevent any ClassCastException s from occurring. 首先,您使用的是原始类型Entry ,而应为Entry<String, String[]> ,这将防止发生任何ClassCastException Next, you'll have to verify that all elements of check are contained in value . 接下来,您必须验证check所有元素都包含在value One easy way is to convert value into a Set to allow for efficient searching: 一种简单的方法是将value转换为Set以允许高效搜索:

List<String> ingredients = List.of(ingredientList);

while (entries.hasNext()) {
    Entry<String, String[]> entry = entries.next();

    String key = entry.getKey();
    String[] value = entry.getValue();

    if (Set.of(value).containsAll(ingredients)) {
        System.out.println("You could make " + key);
    }
}

Note : This compiles with Java 9. If you're using Java 8 or below, you can replace the call to Set#of and List#of with Arrays#asList , but it will be less efficient. 注意 :可以使用Java 9进行编译。如果使用的是Java 8或更低版本,则可以使用Arrays#asList替换对Set#ofList#of Arrays#asList ,但是效率较低。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM