Skip to content
Advertisement

Hasmap merge function value inside an object

I am trying to get a quantity count of products from a list of sales. So what I have is:

JavaScript

The current way I have done it is based on this answer of integers: how to merge more than one hashmaps also sum the values of same key in java

So, right now im traversing a list of sale objects, and for every sale object, check the hasmap if an entry for that product exists, if it does not make one, if it does add how much of the product was sold in current sale to it.

JavaScript

which works, but then i have to convert the hashmap into an arraylist, and add sale details into each entry because i also want to send other sale variales such as productName, not just the id and salecount. Therefore I am trying to find a more efficient way to do this.

This is what im trying to do, i create a new DTO object called productCount, and instead of integers im storing the object in the hasmap.

JavaScript

Advertisement

Answer

Let’s boost the ProductCount class with the constructor and the method:

JavaScript

Now the saleList can be traversed like:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement