Java错误:不兼容的类型:没有类型变量的实例R存在,以便Stream <r>符合布尔值

时间:2017-08-05 12:50:41

标签: java

我是编码的新手 我在java程序中遇到错误,我不知道如何解决。该代码旨在找到素食食谱的平均烹饪时间。正如你在下面看到的,我尝试在isVegetarian()方法中放入一个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));
  }


}

为此我得到错误:

\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

3 个答案:

答案 0 :(得分:0)

问题是map会返回Stream<R>而不是boolean,在您的情况下,您想要的是allMatch而不是filter,而true会返回{ {1}}如果所有元素都满足条件

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

答案 1 :(得分:0)

正如编译器错误所示,类型不匹配。返回的类型为Stream<Boolean>,而不是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)));

答案 2 :(得分:0)

目前你有一个布尔流,但必须将它减少到只有一个布尔值。

要做到这一点,请使用

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

或者更简单一点:

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