Skip to content
Advertisement

How to iterate through ArrayList values of HashMap?

A question from a total newbie. Sorry.

I have this customersOrders HashMap that takes String as keys and ArrayList<Double> as values. I need to find the total sum of orders for each customer and the maximum total sum in order to find the biggest customer. How do I manage to do that using just nested For loops and HashMap methods? I’m totally stuck on that.

JavaScript

So far I’ve been trying to do something like this but obviously with no success:

JavaScript

Advertisement

Answer

You could create another HashMap which keeps your sums and then find the maximum of them.

First iterate through all your HashMap keys and find the sums for each customer like this:

JavaScript

Now finding the maximum should be very straightforward. Try to do that 😀

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