Reporting sums of shopping carts I have an object (ShoppingCart) that has a list of CartItems, which contains the related Product and the quantity bought.

public class ShoppingCart extends BaseEntity {

    @OneToMany(mappedBy = "shoppingCart", cascade = CascadeType.ALL, orphanRemoval = true)
    private List&lt;CartItem&gt; cartItems = new ArrayList&lt;&gt;();

    @DateTimeFormat(pattern = "dd/MM/yyyy hh:MM:ss")
    private LocalDateTime dateTime;

    @Enumerated
    private PaymentMethod paymentMethod = PaymentMethod.CASH;




public class CartItem extends BaseEntity {

    @ManyToOne
    private Product product;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn
    private ShoppingCart shoppingCart;




public class Product extends BaseEntity {

    @NotEmpty
    private String name;

    private double price;

    private int quantity;


I have a view that sums all shopping carts, and I don't know if I should store that sum inside ShoppingCart entity, or iterate to find the sum.
Here's the code, right now I'm iterating.

@GetMapping("/")
    public String getProduct(@ModelAttribute("reportDto") ReportDto reportDto, Model model) {

        double total = 0, cash = 0, credit = 0, debit = 0;
        int quantity;
        List&lt;ShoppingCart&gt; results;
        if (reportDto.getBeginDate() == null || reportDto.getEndDate() == null) {
            results = shoppingCartService.findAll();
        } else {
            results = shoppingCartService.findByDateTimeBetween(reportDto.getBeginDate(), reportDto.getEndDate());
        }
        total = results.stream()
                .mapToDouble(shoppingCart -&gt; shoppingCart.getCartItems().stream()
                        .mapToDouble(cartItem -&gt; cartItem.getProduct().getPrice() * cartItem.getQuantity()).sum())
                .sum();
        quantity = results.stream().mapToInt(
                shoppingCart -&gt; shoppingCart.getCartItems().stream().mapToInt(cartItem -&gt; cartItem.getQuantity()).sum())
                .sum();

        cash = results.stream().filter(shoppingCart -&gt; shoppingCart.getPaymentMethod().isCash())
                .mapToDouble(shoppingCart -&gt; shoppingCart.getCartItems().stream()
                        .mapToDouble(cartItem -&gt; cartItem.getProduct().getPrice() * cartItem.getQuantity()).sum())
                .sum();

        credit = results.stream().filter(shoppingCart -&gt; shoppingCart.getPaymentMethod().isCredit())
                .mapToDouble(shoppingCart -&gt; shoppingCart.getCartItems().stream()
                        .mapToDouble(cartItem -&gt; cartItem.getProduct().getPrice() * cartItem.getQuantity()).sum())
                .sum();

        debit = results.stream().filter(shoppingCart -&gt; shoppingCart.getPaymentMethod().isDebit())
                .mapToDouble(shoppingCart -&gt; shoppingCart.getCartItems().stream()
                        .mapToDouble(cartItem -&gt; cartItem.getProduct().getPrice() * cartItem.getQuantity()).sum())
                .sum();
        model.addAttribute("quantity", quantity);
        model.addAttribute("cash", cash);
        model.addAttribute("credit", credit);
        model.addAttribute("debit", debit);
        model.addAttribute("total", total);
        return "reports/reports";
    }


Also, any feedback regarding the code will be appreciated!
First of all i think making a view in your database which sums your needs will be better because generally sql engine sum operator's performance is way better than lopping values in programming languages.

Second if we go through your code:
You have done mapping for all objects one by one. Java stream mapping is similar to for loops in compile time. So here what you make is similar to creating for loop foreach object which is bad for performance. 
To be performance wise you can create one for loop and generate your sum values in it. 

Note: BigDecimal is the preferred way to store double money values in Java