2017-08-05 35 views
-1

に適合するようにRが存在します。コーディングが新しく、 私には分かりませんの解き方。このコードは、ベジタリアンレシピの調理の平均時間を見つけることを目的としています。以下に示すように、私はlVdaetarian()メソッドの中にラムダ式を入れようとしましたが、問題を理解することはできません。 ありがとうございます。ここでJavaエラー:互換性のない型:変数型のインスタンスがありません<R>がブール値

はプログラムです:

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 

答えて

0

問題はmapはあなたが何をしたいあなたのケースでは、Stream<R>はなくbooleanを返すということですfilterの代わりにallMatchであり、すべての要素が条件を満たす場合にtrueを返す

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

コンパイラのエラーからわかるように、型は一致しません。返される型はBooleanではなく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))); 
+0

また、あなたのアプローチを説明することができ、なぜOPの元のアプローチは動作しませんでした:

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

または少し単純に使用するかということ

を行うには?私はこれがOPのコードを投稿するだけではないと思っています。 – Zabuza

+0

@Zabuzaは答えを投稿して間違って開始します。 –

+1

私はあなたに伝えたいことをかなり得ていませんが、あなたの編集はそれを改善しました。 – Zabuza

0

を現在あなたはboolean型のストリームを持っていますが、ただ一つのブールにそれを軽減する必要があります。それが問題を解決し、なぜ

meatveg = ingredient.stream().noneMatch((theingredients) -> theingredients == Ingredient.BEEF || theingredients == Ingredient.HAM); 
関連する問題