簡體   English   中英

Java錯誤:不兼容的類型:不存在類型變量R的實例,因此Stream <R> 符合布爾值

[英]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);

不兼容的類型:不存在類型變量 F、T 的實例,因此 java.util.Collection<t> 符合 java.util.Set <java.lang.long< div><div id="text_translate"><p> 我正在嘗試將ComplexItem列表轉換為它們對應的 ID Long列表。 但是即使在使用(Collection&lt;ComplexItem&gt;)對getCollection()調用進行類型轉換后,也不會出現上述錯誤 go</p><pre> Set&lt;Long&gt; ids = Collections2.transform( getComplexItems(), new Function&lt;ComplexItem, Long&gt;() { @Override public Long apply(final ComplexItem item) { return item.id; } })); public List&lt;ComplexItem&gt; getComplexItems() {.............. }</pre> </div></java.lang.long<></t>

[英]incompatible types: no instance(s) of type variable(s) F,T exist so that java.util.Collection<T> conforms to java.util.Set<java.lang.Long

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 Java 錯誤:不兼容的類型:不存在類型變量 T 的實例,因此可選<t>符合 Iterable<availablelicence></availablelicence></t> 不兼容的類型:不存在類型變量 T 的實例,因此 Matcher<t> 符合 ArgumentMatcher<long></long></t> 不兼容的類型:不存在類型變量 F、T 的實例,因此 java.util.Collection<t> 符合 java.util.Set <java.lang.long< div><div id="text_translate"><p> 我正在嘗試將ComplexItem列表轉換為它們對應的 ID Long列表。 但是即使在使用(Collection&lt;ComplexItem&gt;)對getCollection()調用進行類型轉換后,也不會出現上述錯誤 go</p><pre> Set&lt;Long&gt; ids = Collections2.transform( getComplexItems(), new Function&lt;ComplexItem, Long&gt;() { @Override public Long apply(final ComplexItem item) { return item.id; } })); public List&lt;ComplexItem&gt; getComplexItems() {.............. }</pre> </div></java.lang.long<></t> Webflux 錯誤 - 不存在類型變量 R 的實例,因此 Flux<uuid> 符合 Mono&lt;&gt;</uuid> 不存在類型變量 V 的實例,因此 ExpectedCondition<boolean> 符合 Function<!--? super WebDriver, V--> 使用 Selenium 3.13.0 時出錯</boolean> 不存在類型變量 V 的實例,因此 ExpectedCondition<Boolean> 符合功能<? super WebDriver, V> 不存在類型變量的實例,因此T符合注釋 Java泛型不兼容類型(不存在類型變量的實例) 不存在類型變量R的實例,因此Observable符合Observable? “更新為RxJava2時,不存在類型變量R的實例,因此Observable符合Observable”錯誤
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM