简体   繁体   English

Java流对象列表

[英]Java streams list of objects

I have troubles using java streams. 我有使用java流的麻烦。 I have two classes: Pizza class 我有两个班:比萨班

public class Pizza {
    private final String name;
    private final List<Ingredient> ingredients;
    // ...
}

and Ingredient class with those : 和成分类与那些:

private final String preetyName;
private final int price;
private final boolean meat;
private final boolean spicy;

I need to use streams but I'm pretty new to this. 我需要使用流,但我对此很新。 First I need to make formatted menu: I have List<Pizza> and after using streams it should return something like this 首先我需要制作格式化的菜单:我有List<Pizza> ,在使用流之后它应该返回这样的东西

pizza_name: ingredient1_preetyname, ingredient2_preetyname...\npizza2_name...

as one string. 作为一个字符串。 I have something like this but It just a string of all ingredients. 我有这样的东西,但它只是一串所有成分。 I dont know how to add pizza name and \\n after ingredients 我不知道如何添加披萨名称和\\ n后成分

String lista=pizzas.stream()
                    .flatMap(p -> p.getIngredients().stream())
                    .map(i ->i.getPreetyName())
                    .collect(Collectors.joining(", "));

2.Second thing is I need to return the cheapest spicy(at least one ingredient is spicy) pizza. 2.第二件事是我需要退回最便宜的辣味(至少一种成分是辣的)披萨。 I know I have to fiter pizzas for spicy ingredients and I know i have to sum ingredients prices but i have honestly no idea how to do that. 我知道我必须为辣的配料配上比萨饼,我知道我必须总结配料价格但我真的不知道该怎么做。

If someone could help my in any possible way, I will be thankful. 如果有人能以任何可能的方式帮助我,我将感激不尽。

You can obtain the String you want with the following: 您可以使用以下命令获取所需的字符串:

String str =
    pizzas.stream()
          .map(p -> p.getName() + ": " + 
                     p.getIngredients().stream()
                                       .map(Ingredient::getPreetyName)
                                       .collect(Collectors.joining(", "))
          )
          .collect(Collectors.joining(System.lineSeparator()));

This creates a Stream<Pizza> of all the pizzas. 这会创建所有比萨饼的Stream<Pizza> For one pizza, we need to map it to the corresponding String representation, which is its name, followed by all of the pretty names of the ingredients joined with a comma. 对于一个披萨,我们需要将它映射到相应的String表示,这是它的名称,然后是用逗号连接的所有漂亮的成分名称。

Finally when we have all those Strings for all pizzas, we can join it again, this time separated with the line separator. 最后,当我们为所有比萨饼提供所有这些字符串时,我们可以再次加入它,这次用行分隔符分隔。


As for your second question about retrieving the cheapest pizza, I will assume that the price of a pizza is the sum of the price of all its ingredients. 至于你关于检索最便宜的披萨的第二个问题,我会假设披萨的价格是其所有成分价格的总和。 In this case, you can filter all pizzas by keeping only the spicy ones (this is obtained by seeing if any ingredients is spicy) and returning the minimum for the comparator comparing the sum of the price of the ingredients of the pizza. 在这种情况下,您可以通过仅保留辛辣的比萨来过滤所有比萨饼(这是通过查看是否有任何成分是辣的而获得)并且比较比较物的最小比较比较比萨饼的成分的价格的总和。

Pizza cheapestSpicy =
    pizzas.stream()
          .filter(p -> p.getIngredients().stream().anyMatch(Ingredient::isSpicy))
          .min(Comparator.comparingInt(
             p -> p.getIngredients().stream().mapToInt(Ingredient::getPrice).sum()
          )).orElse(null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM