[英]Java error: incompatible types: no instance(s) of type variable(s) T exist so that Optional<T> conforms to Iterable<AvailableLicence>
[英]Java Error: incompatible types: no instance(s) of type variable(s) R exist so that Stream<R> conforms to boolean
我是編碼新手,但是在Java程序中遇到錯誤,我不知道如何解決。 該代碼旨在查找素食食譜的平均烹飪時間。 如您在下面看到的,我嘗試將lambda表達式放入isVegetarian()方法中,但似乎無法找出問題所在。 提前致謝。
這是程序:
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
問題是map
返回一個Stream<R>
而不是boolean
,在您的情況下,您想要的是allMatch
而不是filter
,如果所有元素都滿足條件,則返回true
meatveg = ingredient.stream( )
.allMatch((theingredients) -> !( theingredients == Ingredient.BEEF || theingredients == Ingredient.HAM ));
正如編譯器錯誤提示的那樣,類型不匹配。 返回的類型為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)));
當前,您有一個布爾流,但必須將其減少為一個布爾值。
為此,請使用
meatveg = ingredient.stream().allMatch((theingredients) -> !( theingredients == Ingredient.BEEF || theingredients == Ingredient.HAM ));
或更簡單:
meatveg = ingredient.stream().noneMatch((theingredients) -> theingredients == Ingredient.BEEF || theingredients == Ingredient.HAM);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.