我正在尝试建立一个搜索功能,用户可以根据用户输入来搜索食物。 HashMap中的项目设置为具有Food类对象和对象本身的UUID。这是食品的仓库import java.util.Arrays;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;public class FoodRepository { // Create a hash map private HashMap foods = new HashMap(); // Constructor public FoodRepository () { //Food Objects NutritionFacts cokeNutritionFacts = new NutritionFacts(190, 0, 60, 54, 54, 0); String[] cokeTags = {"Drink", "Cola", "Coca-Cola"}; Food coke = new Food("Coca-Cola", cokeNutritionFacts, cokeTags); foods.put(coke.getUUID(), coke); NutritionFacts drpepperNutritionFacts = new NutritionFacts(180, 0, 75, 48, 47, 0); String[] drpepperTags = {"Drink", "Cola", "Dr Pepper"}; Food drpepper = new Food( "Dr Pepper", drpepperNutritionFacts, drpepperTags); foods.put(drpepper.getUUID(), drpepper); NutritionFacts cfaSandwichNutritionFacts = new NutritionFacts(440, 19, 1350, 40, 5, 28); String[] cfaSandwichTags = {"Entree", "Sandwich", "Dinner", "Lunch", "Chick Fil A"}; Food cfaSandwich = new Food( "Chick Fil A Chicken Sandwich", cfaSandwichNutritionFacts, cfaSandwichTags); foods.put(cfaSandwich.getUUID(), cfaSandwich); NutritionFacts bigMacNutritionFacts = new NutritionFacts(540, 28, 940, 42, 9, 25); String[] bigMacTags = {"Entree", "Burger", "Dinner", "Lunch", "McDonalds"}; Food bigMac = new Food( "McDonalds Big Mac", bigMacNutritionFacts, bigMacTags); foods.put(bigMac.getUUID(), cfaSandwich); // Get a set of the entries Set foodSet = foods.entrySet(); } // Getter methods public HashMap getFoods() { return foods; } // Setter methods public void setFoods(HashMap foods) { this.foods = foods; }}这是Food类,它定义进入HashMap的对象(省略了getters和setters)public class Food { // Class variables private String uuid; private String name; private NutritionFacts nutritionFacts; private String[] tags; // Constructor public Food(String name, NutritionFacts nutritionFacts, String[] tags) { this.uuid = UUID.randomUUID().toString(); this.name = name; this.nutritionFacts = nutritionFacts; this.tags = tags; }}最后,这是营养事实课public class NutritionFacts { // Class Variables private int calories; private int fat; private int sodium; private int carbs; private int sugar; private int protein; //private int iron; //private int calcium; //private int vitaminA; //private int VitaminC; //Constructor public NutritionFacts(int calories, int fat, int sodium, int carbs, int sugar, int protein){ this.calories = calories; this.fat = fat; this.sodium = sodium; this.carbs = carbs; this.sugar = sugar; this.protein = protein; }}注意,如果太多,我很抱歉;这是我的搭档,也是我第一次使用android studio,而我们俩都对Java没有任何经验。我们一直在考虑以下方面的工作可能会起作用:int caloriesConsumed = 0;String searchValue = scan.nextString();boolean flag = Arrays.asList(foods.get(key).tags).contains(searchValue); if(flag == true) { caloriesConsumed = (caloriesConsumed + xxx;}但是问题是,如果用户使用“汉堡”之类的标签搜索食品,希望显示一个大型Mac,则该方法需要知道密钥并在代码中;因此,破坏了追求价值的目的。另外,我们已经看到有些人使用这样的方法:String needle = "burger"for(Map.Entry<String, Food> entry : foods.entrySet()) { Food v = entry.getValue(); if(v.contains(needle)) caloriesConsumed = (caloriesConsumed + xxx); }但是.contains()方法不适用于字符串/不兼容,因为HashMap的值是一个特殊的对象,而不是常规的数据类型。感谢您提供的所有帮助,非常感谢! 最佳答案 最后一个片段可能是一个可行的解决方案。if(v.contains(needle)){ caloriesConsumed = (caloriesConsumed + xxx);}在这里,v是食物的一种,因此很明显它没有包含方法,因为您没有实现它。如我所见,您需要在v.tags字段中进行搜索,但这是一个字符串数组,其中没有contains方法。我建议将标签字段的类型从String []更改为List ,然后可以使用tags.contains(needle)在标签中查找指定的搜索项。编辑:对于Map 问题在您的代码中,我看到您使用原始的HashMap类型// Create a hash mapprivate HashMap foods = new HashMap();问题是,以这种方式,没有人知道这是一个String,Food哈希图,因此,如果您想将其分配给String,Food条目,显然会得到错误。我建议这样使用它: 私有Map foods = new HashMap ();还要将getter setter更改为return / accept Map 注意:在编程中,使用接口而不是具体类型是一种更好的方法,但是如果您仍然想指定这是一个HashMap(我看不到为什么,但是假设),则将变量声明为HashMap 食品那应该做的工作:)
08-05 03:54