java - 运算符 '!=' 不能应用于 java
问题描述
嗨,我的 if 语句无法正常工作。
if (foods.iterator().next() != Meal.LUNCH)
在这一行中,我收到一条错误消息,提示 Operator '!=' cannot be applied to 'comp1110.exam.Q1MealPlan.Food', 'comp1110.exam.Q1MealPlan.Meal'
public static Set<List<Food>> getAllMealPlans(Set<Food> foods, int numMeals) {
Set<List<Food>> set = new HashSet<>();
List<Food> aList = new ArrayList<Food>();
System.out.println(aList);
System.out.println(foods.iterator().next().meal);
if (foods.iterator().next().meal == Meal.BREAKFAST){
aList.add(foods.iterator().next());
set.remove(foods.iterator().next());
if (foods.iterator().next() != Meal.LUNCH){
}
}
解决方案
我创建了一个测试类,它应该可以帮助您解决问题,您的代码中有几个问题。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
new Test().test();
}
void test() {
Set<Food> foods = new HashSet<>();
foods.add(new Food("a", Meal.BREAKFAST));
foods.add(new Food("b", Meal.BREAKFAST));
foods.add(new Food("c", Meal.LUNCH));
Set<List<Food>> mealPlans = getAllMealPlans(foods, 0);
mealPlans.forEach(f -> System.out.println(Arrays.toString(f.toArray())));
System.out.println("--------------------");
//I think what you might want is a Map from Meal to List of Foods
Map<Meal, List<Food>> mealPlans2 = getAllMealPlansMap(foods);
mealPlans2.entrySet().forEach(f ->{
System.out.println("Meal: "+f.getKey());
System.out.println(Arrays.toString(f.getValue().toArray()));
System.out.println("");
});
}
public static Set<List<Food>> getAllMealPlans(Set<Food> foods, int numMeals) { //numMeals unused?
Set<List<Food>> set = new HashSet<>();
Iterator<Food> iterator = foods.iterator(); // only instantiate the iterator once(!)
while (iterator.hasNext()) {
Food food = iterator.next(); //only call .next once(!)
List<Food> aList = new ArrayList<Food>();
if (food.meal == Meal.BREAKFAST) {
//set.remove(foods.iterator().next()); //why remove it from the set?.. and by foods.iterator() you create a NEW Iterator which is starting at index 0 again .. and check that you are calling .remove with the correct type
aList.add(food);
if (food.meal != Meal.LUNCH) {
//
}
}
if(!aList.isEmpty()) {//only add the list if its not empty
set.add(aList);
}
}
return set;
}
public static Map<Meal,List<Food>> getAllMealPlansMap(Set<Food> foods) {
return foods.stream().collect(Collectors.groupingBy(f -> f.meal));
}
enum Meal {
LUNCH, BREAKFAST
}
class Food {
String name;
Meal meal;
public Food(String name, Meal meal) {
super();
this.name = name;
this.meal = meal;
}
@Override
public String toString() {
return name + " " + meal;
}
}
}
推荐阅读
- websphere - IBM Watson Assistant 过滤不同的元素
- node.js - 如何在nodejs web3中连接到bnb
- python - ImportError:尝试在没有已知父包的情况下进行相对导入?
- java - Java maven项目,构建失败版本不匹配
- spring-boot - 如何通过spring b使用roleID和secretID从保险库访问凭据?
- python - python打印以下输出
- r - 在 dplyr 中从变量中引用列名
- typescript - Typescript/cypress-cucumber-preprocessor :使用动态示例编写场景大纲
- multithreading - 两个类之间的非法跨线程操作。无法区分线程以解决问题
- c++ - GetWindowDC 工作不正常