Home > Blockchain >  Using Stream to sum Object feilds in List
Using Stream to sum Object feilds in List

Time:10-27

I'm trying to sum the fields of a List and return the value. I wanted to use a stream for this but I'm new to streams and am not sure if a stream can accomplish this or not. This is what I've tried but I think the syntax is incorrect.

    public double calculateCartTotal(ArrayList cartItems) {
        
        this.totalPrice = cartItems.stream()
                .map(item -> item.getTotalItemPrice())
                .reduce(0, (a, b) -> a   b);
        

        return totalPrice;
        
    }

Relevant class structure for context.

public class Cart {

private double totalPrice;
private List<CartItem> cartItems;

public Cart() {
        super();
        this.totalPrice = 0;
        this.cartItems = new ArrayList<CartItem>();
    }
   //other methods
}


public class CartItem {

    private Product productName;
    private int numberOfUnits;
    private double totalItemPrice;
    private double unitPrice;

    public CartItem(Product productName, int numberOfUnits) {
        super();
        this.productName = productName;
        this.numberOfUnits = numberOfUnits;
    }
    //other methods

}

Get total and unit price methods


public double getTotalItemPrice() {
        return this.getUnitPrice() * numberOfUnits;

    }

    public double getUnitPrice() {
        return Double.parseDouble(productName.getCurrentPrice());
    }

CodePudding user response:

You need to declare the cartItems parameter as a List<CartItem>:

public double calculateCartTotal(List<CartItem> cartItems) {

    this.totalPrice = cartItems.stream()
           .mapToDouble(CartItem::getTotalItemPrice)
           .sum();
    return totalPrice;

}

CodePudding user response:

Your code has 2 problems.

  1. Missing type parameter of ArrayList. This is problematic, because now we don't know if the list actually contains CartItems. Also you usually want to avoid using the implementation of collections for the declaration e.g. List<CartItem> items = new ArrayList<>(); is much better.

  2. Not converting the stream to a DoubleStrem. Using a DoubleStream has the advantage, that it doesn't convert your primitive double to a Double object. Additionally it's made to work with number unlike the normal Stream so it comes with useful methods like sum and we don't have to use reduce.

Example code

public double calculateCartTotal(List<CartItem> cartItems) {
    this.totalPrice = cartItems.stream()
        .mapToDouble(i -> i.getTotalItemPrice())
        .sum();
    return totalPrice;  
}
  • Related