Zoo tutorials: [ SQL | Linux | XML ]
ProgZoo: [ Java | C# | VB | C++ | Perl ]
Log in

A Gentle Introduction to
C++ Programming

Tutorial: Creating a TreeMap

 

The TreeMap keeps its keys in order. We create TreeMap objects and use them to accumulate values.

1. List regions.


Big

The program shown creates a new TreeMap called rList. We are not using the value at all, only the keySet is of interest.

Print each region.

2. The total population of each region.


Big

Each value of the rList TreeMap accumulates the total population. We initialise each value as 0L if required.

Print the total population of each region.

Notice how we initialise each region once - when we come across a region that is not in the TreeMap already:

if (!rList.containsKey(c.region))
  rList.put(c.region,0L);