gpt4 book ai didi

Java 错误 : incompatible types: no instance(s) of type variable(s) R exist so that Stream conforms to boolean

转载 作者:行者123 更新时间:2023-12-05 05:19:49 25 4
gpt4 key购买 nike

我是编码新手我在 java 程序中遇到错误,我不知道如何解决。该代码旨在查找素食食谱的平均 cooking 时间。正如您在下面看到的,我尝试在 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

最佳答案

正如编译器错误提示的那样,类型不匹配。返回的类型是 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)));

关于Java 错误 : incompatible types: no instance(s) of type variable(s) R exist so that Stream<R> conforms to boolean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45522121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com