Java错误:不兼容的类型:不存在类型变量R的实例,因此流<;R>;符合布尔值

Java错误:不兼容的类型:不存在类型变量R的实例,因此流<;R>;符合布尔值,java,Java,我不熟悉编码和编程 我在java程序中遇到一个错误,我不知道如何解决。该代码旨在查找素食食谱的平均烹饪时间。正如您在下面看到的,我尝试在is素食()方法中放入lambda表达式,但似乎无法解决问题。 提前谢谢 节目如下: public enum Ingredient { BEEF, HAM, APPLES, PEAS, CARROTS; } import java.util.ArrayList; public class Recipe { public String name;

我不熟悉编码和编程 我在java程序中遇到一个错误,我不知道如何解决。该代码旨在查找素食食谱的平均烹饪时间。正如您在下面看到的,我尝试在is素食()方法中放入lambda表达式,但似乎无法解决问题。 提前谢谢

节目如下:

public enum Ingredient
{
BEEF, HAM, APPLES, PEAS, CARROTS;
}
   import java.util.ArrayList; 

public class Recipe
{
    public String name;
    public int time;
    public ArrayList<Ingredient> ingredient;
    public boolean meatveg;


    public int getTime( )
    {
        return time;
    }
    public boolean isVegetarian( )
    {
        meatveg = ingredient.stream( )
                    .filter((theingredients) -> !( theingredients == Ingredient.BEEF || theingredients == Ingredient.HAM ))
                    .map((theingredients) -> true );
        return meatveg;
    }

    public Recipe( String name, ArrayList<Ingredient> ingredient, int time )
    {
        this.name = name;
        this.ingredient = ingredient;
        this.time = time;

    }
}
import java.util.ArrayList;

public class Recipes {

  public static void main(String[] args) {
    ArrayList<Recipe> recipes = new ArrayList<>();


    fillTheList(recipes);

    int total = recipes.stream()
        .filter((recipe) -> recipe.isVegetarian())
        .map((recipe) -> recipe.getTime())
        .reduce(0, (time1, time2) -> time1 + time2);

    int count = recipes.stream()
        .filter((recipe) -> recipe.isVegetarian())
        .map((recipe) -> 1)
        .reduce(0, (value1, value2) -> value1 + value2);

    System.out.println(total / (double) count);
  }


  static void fillTheList(ArrayList recipes) {
    ArrayList<Ingredient> ingredients = new ArrayList<>();
    ingredients.add(Ingredient.BEEF);
    ingredients.add(Ingredient.HAM);
    ingredients.add(Ingredient.APPLES);
    recipes.add(new Recipe("Recipe 1", ingredients, 400));

    ingredients = new ArrayList<>();
    ingredients.add(Ingredient.PEAS);
    ingredients.add(Ingredient.CARROTS);
    recipes.add(new Recipe("Recipe 2", ingredients, 200));

    ingredients = new ArrayList<>();
    ingredients.add(Ingredient.PEAS);
    ingredients.add(Ingredient.APPLES);
    recipes.add(new Recipe("Recipe 3", ingredients, 300));
  }


}
公共枚举成分
{
牛肉、火腿、苹果、豌豆、胡萝卜;
}
导入java.util.ArrayList;
公共课食谱
{
公共字符串名称;
公共整数时间;
公共ArrayList成分;
公共蔬菜;
公共整型getTime()
{
返回时间;
}
公共布尔值是素食者()
{
Meatvege=配料。流()
.filter((配料)->!(配料==配料.牛肉| |配料==配料.火腿))
.map((元素)->true);
返回肉类蔬菜;
}
公共配方(字符串名称、ArrayList成分、整数时间)
{
this.name=名称;
这个成分=成分;
这个时间=时间;
}
}
导入java.util.ArrayList;
公共类食谱{
公共静态void main(字符串[]args){
ArrayList recipes=新的ArrayList();
肉馅清单(食谱);
int total=recipes.stream()
.filter((配方)->recipe.is素食者())
.map((配方)->recipe.getTime())
.减少(0,(time1,time2)->time1+time2);
int count=recipes.stream()
.filter((配方)->recipe.is素食者())
.map((配方)->1)
.减少(0,(值1,值2)->值1+值2);
系统输出打印项次(总/(双)计数);
}
静态空白填充列表(ArrayList配方){
ArrayList Components=新的ArrayList();
配料。添加(配料。牛肉);
配料:添加(配料:火腿);
配料。添加(配料。苹果);
配方。添加(新配方(“配方1”,配料,400));
配料=新的ArrayList();
配料:添加(配料:豌豆);
配料:添加(配料:胡萝卜);
配方。添加(新配方(“配方2”,配料,200));
配料=新的ArrayList();
配料:添加(配料:豌豆);
配料。添加(配料。苹果);
配方。添加(新配方(“配方3”,配料,300));
}
}
为此,我得到了一个错误:

\Recipe.java:19: error: incompatible types: no instance(s) of type variable(s) R exist so that Stream<R> conforms to boolean
                                        .map((theingredients) -> true );
                                            ^
  where R,T are type-variables:
    R extends Object declared in method <R>map(Function<? super T,? extends R>)
    T extends Object declared in interface Stream
Note: Recipes.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
\Recipe.java:19:错误:不兼容类型:不存在类型变量R的实例,因此流符合布尔值
.map((元素)->true);
^
其中R,T是类型变量:

R扩展方法映射中声明的对象(函数问题是
map
返回
流而不是
布尔值
,在您的情况下,您需要的是
allMatch
,而不是
filter
,如果所有元素都满足条件,它将返回
true

meatveg = ingredient.stream( )
            .allMatch((theingredients) -> !( theingredients == Ingredient.BEEF || theingredients == Ingredient.HAM ));

正如编译器错误所提示的,类型不匹配。返回的类型是
Stream
,而不是
Boolean

为了完成您想要做的事情,您需要利用
anyMatch
方法

Boolean meatveg = ingredient.stream( )
  .anyMatch(i -> !( i == Ingredient.BEEF || i == Ingredient.HAM ));
您可以通过使用
Predicate.isEqual
并执行静态导入,以稍微更可读的方式编写:

Boolean meatveg = ingredients.stream()
      .anyMatch(isEqual(BEEF).or(isEqual(HAM)));

目前,您有一个布尔值流,但必须将其减少为一个布尔值

要做到这一点,请使用

meatveg = ingredient.stream().allMatch((theingredients) -> !( theingredients == Ingredient.BEEF || theingredients == Ingredient.HAM ));
或者简单一点:

meatveg = ingredient.stream().noneMatch((theingredients) -> theingredients == Ingredient.BEEF || theingredients == Ingredient.HAM);

你能解释一下你的方法吗?为什么它解决了这个问题,为什么OP的原始方法不起作用?我认为这对OP的帮助不仅仅是发布一些代码。@Zabuza发布答案是错误的开始。我不太明白你想说什么,但你的编辑改进了它,很好。