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.