Home » Java » java – Problem with ArrayList and Stream… I have to stream an ArrayList with elements that have attributes in another ArrayList-Exceptionshub

java – Problem with ArrayList and Stream… I have to stream an ArrayList with elements that have attributes in another ArrayList-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have the Lists and … Now , I have to stream the order list with the products and their names and numbers. I will write a simplified code cause the actual one is pretty long

ArrayList<Order> order = new ArrayList<>();
order.add(new Order(011, 579, 1 , true);

ArrayList<Product> prod = new ArrayList<>();
prod.add(new Product(9918, "Nvidia Graphic Card", 1299);

the goal is to stream the order with the true boolean (which in this case means is confirmed) and with the name and number of the products in those orders….

order.stream().filter(e-> e.isConfirmed()).forEach((v) -> System.out.println("\n \t "+ v));

but this is just the order, I do not know how to get the product inside the same stream… Thanks

How to&Answers:

Here is one way of doing this, but be warned that it is terrible ineffective solution as for each confirmed order it has to scan products list to find the valid product.
Therefore I would strongly recommend you to consider using another data structure (like Map<Integer, Product> which reduces complexity of finding a product from O(n) to O(log n)) to store products or change the Order class to store Product (or even List<Product>) directly instead of storing the productId.

public static void main(String[] args) {
    List<Order> orders = new ArrayList<>(List.of(
            new Order(1, 1, 5, false),
            new Order(2, 2, 5, true),
            new Order(3, 3, 7, true),
            new Order(4, 1, 1, true),
            new Order(5, 2, 3, true),
            new Order(6, 3, 5, false)
    ));

    List<Product> products = new ArrayList<>(List.of(
            new Product(1, "Product 1", 1.99),
            new Product(2, "Product 2", 2.99),
            new Product(3, "Product 3", 3.99)
    ));

    String confirmedOrders = orders.stream()
            .filter(Order::isConfirmed)
            .map(order -> products.stream()
                    .filter(product -> product.getId() == order.getProductId())
                    .findFirst()
                    .map(getOrderSummaryString(order))
                    .orElseThrow(() -> new IllegalArgumentException(
                            "Invalid product id (#" + order.getProductId() + ") in order #" + order.getId()))
            )
            .collect(Collectors.joining(System.lineSeparator()));

    System.out.println("Confirmed orders list:" + System.lineSeparator() + confirmedOrders);
}

private static Function<Product, String> getOrderSummaryString(Order order) {
    return product ->
            "\t Order #" + order.getId() + " of " + order.getQuantity() + " * " + product.getName() +
                    " ($" + product.getPrice() + ")," + " $" +
                    String.format("%.2f", product.getPrice() * order.getQuantity());
}

Output:

Confirmed orders list:
     Order #2 of 5 * Product 2 ($2.99), $14.95
     Order #3 of 7 * Product 3 ($3.99), $27.93
     Order #4 of 1 * Product 1 ($1.99), $1.99
     Order #5 of 3 * Product 2 ($2.99), $8.97

Live demo