に適合するように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
また、あなたのアプローチを説明することができ、なぜOPの元のアプローチは動作しませんでした:
または少し単純に使用するかということ
を行うには?私はこれがOPのコードを投稿するだけではないと思っています。 – Zabuza
@Zabuzaは答えを投稿して間違って開始します。 –
私はあなたに伝えたいことをかなり得ていませんが、あなたの編集はそれを改善しました。 – Zabuza