I have a Map<String, Item> , where Item is the entities that I need to assemble into a separate List<Item> . I try to collect the necessary Item in the formOrders method:

 public class InvoiceData { static final Vendor vendor = Vendor.getInstance(); private List<Invoice> orders; private List<Customer> customers; private Map<String, Item> items; public List<Invoice> getOrders() { return orders; } public InvoiceData() { orders = formOrders(); } //...сustomerSupplier, itemSupplier... public List<Invoice> formOrders() { // Список заказов. Сюда, собственно, будут собираться объекты Invoice. List<Invoice> result = new ArrayList<>(); customers = customerSupplier.get(); // Покупатели items = itemSupplier.get(); // Товары customers.forEach(customer -> { Invoice invoice = new Invoice(); result.add(invoice); invoice.setVendor(vendor); invoice.setRecipient(customer); invoice.setTax(0.2); invoice.setItems(items.entrySet().stream() .filter(item -> item.getKey().equals(customer.getId())) .map(Map.Entry::getValue) .collect(Collectors.toList())); }); return result; } } 

The list of orders itself consists of objects of the Invoice class:

 public class Invoice { private Vendor vendor; private Customer recipient; private List<Item> items; private BigDecimal priceOverall; private Double tax; public Invoice() { } // ...get и set... } 

In the class InvoiceData there is a "filling" with data. In the future, an object of this class will be used to generate reports (dynamicreports). customerSupplier and itemSupplier provide a constant set of elements to initialize the customers and items lists, respectively.

Simply put: I take a specific customer (Customer) and find all the products he ordered (stored in the Map).

Since I started familiarizing myself with the Java Stream API recently, I do not quite understand how to collect elements from Map to List using Stream . Do I need to write my own Collector , or is there a simpler solution? I will be glad to any help.

  • xxxSupplier - are these some Optionals? - Oleksiy Morenets
  • Unfortunately, I do not know what Optional is. A quick Google search prompts the idea that my suppliers are not Optional. These are implementations of the Supplier's functional interface, both returning a List of objects of a certain class (Customer and Item, respectively) - Oriant
  • Do you filter all MapEntry <Item> by key, which should match your customer's ID, right? Then this operation will produce a maximum of 1 entry if the key matches. And then you collect with pom collectors actually nothing. I suspect that you somehow did not quite correctly organized the storage of your orders, customers and item. Or in 2 words (or better with a code! !!!) describe what should happen and where it is stored. UPD: by the way, in the filter, the word item is confusing, because you work with entry, and the Item class has nothing to do with this. - Oleksiy Morenets
  • What logically represents the string key of your map? - Oleksiy Morenets
  • The key is the buyer's id (string) - Oriant

1 answer 1

If you simply take some keys from the map (for example, filtered by some predicate), and collect the values ​​in a sheet, then something like this:

 itemSupplier.get().entrySet().stream() .filter(entry -> entry.getKey().equals(customer.getId())) .map(Map.Entry::getValue) .collect(Collectors.toList()) ); 

But it is not clear what you want to do next with the invoice and why the orders sheet was created ...

  • I independently came to the decision completely identical to yours, but there was a new problem. Each customer may have several ordered products, but this filtering provides only 1 product (only one is added to the shopping list instead of several necessary goods) - Oriant
  • So I wrote about the same in the comments above. Describe the storage structure for your system. As I understand it, there must be a list of customers, each of which stores a list of orders, each of which stores a list of goods. You obviously do not. But as??? - Oleksiy Morenets
  • If itemSupplier returns Map<String, List<Item>> instead of Map<String, Item> , how to pull out the sheet I need from the Map correctly? A similar filtering variant produces an error, the essence of which is not very clear to me: incompatible types: inference variable T has incompatible bounds equality constraints: org.hillel_elem.homework_5.Item lower bounds: java.util.List <org.hillel_elem.homework_5.Item> - Oriant
  • Then no dancing is needed. Just from itemSupplier, call get (customerId) and get the required sheet. - Oleksiy Morenets
  • invoice.setItems (items.get (customer.getId ())); - Your sheet is the value that corresponds to the key, and the key in this case is the same (customer.getId ()) - Oleksiy Morenets