Accumulating parcels by destination

A warehouse records batches of parcels sent to three destinations. An initially empty map, totals, associates each destination with its accumulated number of parcels.


Batch

Destination

Parcels

1

North

8

2

South

5

3

North

4

4

East

6

5

South

7

6

North

3

7

East

9


For each batch, the program performs these operations in order:


Plain text
previous = totals.getOrDefault(destination, 0)
totals.put(destination, previous + parcels)


Here, getOrDefault returns the stored value when the key exists and otherwise returns the supplied default without changing the map. put inserts an association or replaces its existing value.


Complete the final map values as exact integers representing numbers of parcels.

DestinationFinal parcel total
North
South
East

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Map ADT Questions: