Java streams list of objects

Molioo

I have troubles using java streams. 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

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

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. 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.

Tunaki

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. 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.

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);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get List of List of Objects with Java Streams

From Dev

Java streams collect list of objects to buckets

From Dev

Transform a flat list to domain objects with child objects using java streams

From Java

Split list of objects into multiple lists of fields values using Java streams

From Dev

Getting only required objects from a list using Java 8 Streams

From Dev

Modify property value of the objects in list using Java 8 streams

From Dev

How to get a Set from a list of objects using Java Streams

From Dev

Split list of objects into multiple lists of fields values using Java streams

From Dev

How to create a list of distinct objects using list of different objects having properties using java streams

From Dev

Java Streams and List of List of List

From Dev

Using Java Streams to group together a List of objects by an attribute and reduce them to a new list of object with the average of another attribute

From Dev

How to use java 8 streams to make a new list by using another's list objects values with filter?

From Dev

Using Java8 Streams to create a list of objects from another two lists

From Dev

How to Group Objects in a List into other Lists by Attribute using streams & Java 8?

From Dev

How to match objects from list with data from map with condition and save to another map using Java 8 Streams

From Dev

Convert a List of objects to a Map using Streams

From Dev

Print list items with Java streams

From Dev

Parsing a list using java streams

From Dev

Print list items with Java streams

From Dev

Java 8 Streams : build multilevel / composite objects

From Dev

Java 8 streams nonNull on properties of objects

From Dev

Java streams: Map<Enum, List<A>> to List<B>

From Dev

List to Map of List using java streams

From Dev

Converting Java List to another using java streams

From Dev

processing list of objects in Java

From Dev

Aggregate List of objects in Java

From Dev

Aggregate List of objects in Java

From Dev

Converting a list of objects to a list of optional objects in Java

From Java

How to join list of non-string objects using streams

Related Related

  1. 1

    How to get List of List of Objects with Java Streams

  2. 2

    Java streams collect list of objects to buckets

  3. 3

    Transform a flat list to domain objects with child objects using java streams

  4. 4

    Split list of objects into multiple lists of fields values using Java streams

  5. 5

    Getting only required objects from a list using Java 8 Streams

  6. 6

    Modify property value of the objects in list using Java 8 streams

  7. 7

    How to get a Set from a list of objects using Java Streams

  8. 8

    Split list of objects into multiple lists of fields values using Java streams

  9. 9

    How to create a list of distinct objects using list of different objects having properties using java streams

  10. 10

    Java Streams and List of List of List

  11. 11

    Using Java Streams to group together a List of objects by an attribute and reduce them to a new list of object with the average of another attribute

  12. 12

    How to use java 8 streams to make a new list by using another's list objects values with filter?

  13. 13

    Using Java8 Streams to create a list of objects from another two lists

  14. 14

    How to Group Objects in a List into other Lists by Attribute using streams & Java 8?

  15. 15

    How to match objects from list with data from map with condition and save to another map using Java 8 Streams

  16. 16

    Convert a List of objects to a Map using Streams

  17. 17

    Print list items with Java streams

  18. 18

    Parsing a list using java streams

  19. 19

    Print list items with Java streams

  20. 20

    Java 8 Streams : build multilevel / composite objects

  21. 21

    Java 8 streams nonNull on properties of objects

  22. 22

    Java streams: Map<Enum, List<A>> to List<B>

  23. 23

    List to Map of List using java streams

  24. 24

    Converting Java List to another using java streams

  25. 25

    processing list of objects in Java

  26. 26

    Aggregate List of objects in Java

  27. 27

    Aggregate List of objects in Java

  28. 28

    Converting a list of objects to a list of optional objects in Java

  29. 29

    How to join list of non-string objects using streams

HotTag

Archive