我已经制作了一个简短的功能示例。我将一些流封装在Pizza类中,以提高可读性。
成分
public class Ingredient {
private String name;
private double price;
private boolean meat;
public Ingredient(String name, double price, boolean meat) {
this.name = name;
this.price = price;
this.meat = meat;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isMeat() {
return meat;
}
public void setMeat(boolean meat) {
this.meat = meat;
}
}
披萨
public class Pizza {
private String name;
private List<Ingredient> ingredients;
public Pizza(String name, List<Ingredient> ingredients) {
this.name = name;
this.ingredients = ingredients;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Ingredient> getIngredients() {
return ingredients;
}
public void setIngredients(List<Ingredient> ingredients) {
this.ingredients = ingredients;
}
public boolean isVegan() {
return (ingredients != null) ? ingredients.stream().noneMatch(Ingredient::isMeat)
: false;
}
public double getTotalCost() {
return (ingredients != null) ? ingredients.stream().map(Ingredient::getPrice)
.reduce(0.0, Double::sum)
: 0;
}
@Override
public String toString() {
return "Pizza [name=" + name + "; cost=" + getTotalCost() +"$]";
}
}
主要内容
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
public class VeganPizzaPlace {
public static void checkMostExpensiveVeganPizza(List<Pizza> pizzas) {
if (pizzas != null) {
Optional<Pizza> maxVegan =
pizzas.stream()
.filter(Pizza::isVegan)
.max(Comparator.comparingDouble(Pizza::getTotalCost));
if (maxVegan.isPresent()) {
System.out.println(maxVegan.get().toString());
} else {
System.out.println("No vegan pizzas in the menu today");
}
}
}
public static void main (String[] args) {
List<Pizza> pizzas = new ArrayList<Pizza>();
Ingredient tomato = new Ingredient("tomato", 0.50, false);
Ingredient cheese = new Ingredient("cheese", 0.75, false);
Ingredient broccoli = new Ingredient("broccoli", 50.00, false);
Ingredient ham = new Ingredient("ham", 10.00, true);
List<Ingredient> ingredientsMargherita = new ArrayList<Ingredient>();
ingredientsMargherita.add(tomato);
ingredientsMargherita.add(cheese);
Pizza margherita = new Pizza("margherita", ingredientsMargherita);
List<Ingredient> ingredientsSpecial = new ArrayList<Ingredient>();
ingredientsSpecial.add(tomato);
ingredientsSpecial.add(cheese);
ingredientsSpecial.add(broccoli);
Pizza special = new Pizza("special", ingredientsSpecial);
List<Ingredient> ingredientsProsciutto = new ArrayList<Ingredient>();
ingredientsProsciutto.add(tomato);
ingredientsProsciutto.add(cheese);
ingredientsProsciutto.add(ham);
Pizza prosciutto = new Pizza("prosciutto", ingredientsProsciutto);
pizzas.add(margherita);
pizzas.add(special);
pizzas.add(prosciutto);
checkMostExpensiveVeganPizza(pizzas);
}
}
输出
披萨 [名称=special; 价格=51.25美元]
如果您不喜欢干净的代码,可以使用以下代码代替
Optional<Pizza> maxVegan =
pizzas.stream()
.filter(p -> p.getIngredients().stream().noneMatch(Ingredient::isMeat))
.reduce((p1, p2) -> p1.getIngredients().stream().map(Ingredient::getPrice).reduce(0.0, Double::sum)
< p2.getIngredients().stream().map(Ingredient::getPrice).reduce(0.0, Double::sum) ? p1 : p2);
编辑:使用reduce
选择最大值披萨的表达式基于 Urma、Fusco 和 Mycroft 的书《Java 8实战》(第110页,列表5.8)。这是一本非常好的书!:-)
Stream.max
еСҐпЉЯ - lexicore(Pizza, price)
对。通过仔细编写比较器,您可以完美地找到最高价格的素食披萨。 - fps