Difference between revisions of "Creating TreeMap Tutorial"

From Progzoo
Jump to: navigation, search
(Show the total population of each region)
(List large regions.)
 
Line 352: Line 352:
 
{
 
{
 
   TreeMap<String,Long> regionList = new TreeMap<String,Long>();
 
   TreeMap<String,Long> regionList = new TreeMap<String,Long>();
   for(Country c:world.values()){
+
   for(Country c:world.values())
 +
  {
 
     if(!regionList.containsKey(c.region))
 
     if(!regionList.containsKey(c.region))
 +
    {
 
       regionList.put(c.region,0L);
 
       regionList.put(c.region,0L);
 +
    }
 
     regionList.put(c.region,
 
     regionList.put(c.region,
 
                   regionList.get(c.region)+c.pop);
 
                   regionList.get(c.region)+c.pop);
 
   }
 
   }
 
   for(String s:regionList.keySet())
 
   for(String s:regionList.keySet())
 +
  {
 
     if (regionList.get(s)>1E8)
 
     if (regionList.get(s)>1E8)
 +
    {
 
       System.out.printf("%-35s%13d\n",s,regionList.get(s));
 
       System.out.printf("%-35s%13d\n",s,regionList.get(s));
 +
    }
 +
  }
 
}
 
}
 
]]></answer>
 
]]></answer>
 
</question>
 
</question>
 
{{TreeMap Tutorial}}
 
{{TreeMap Tutorial}}

Latest revision as of 20:40, 12 September 2012

We create our own TreeMap objects.

Print every region once

To print each region we must create our own list regionList. As we process each country we add the region to our list. The value we put in does not matter, we can use anything. Some regions will be added many times - this doesn't matter each simply over-writes the previous.

Print each region just once.

[Font] [Default] [Show] [Resize] [History] [Profile]

The number of regions.


Print the number of regions.

[Font] [Default] [Show] [Resize] [History] [Profile]

Number of countries in each region.

The program shown creates a new TreeMap called rList - we put in every region of the world.

Print the number of countries in each region. Use 35 characters for the region and 3 characters for the number.


[Font] [Default] [Show] [Resize] [History] [Profile]

Number of large countries in each region.

For each region show the number of countries with a population of at least 10 million.


[Font] [Default] [Show] [Resize] [History] [Profile]

Show the total population of each region

For each region show the total population.


[Font] [Default] [Show] [Resize] [History] [Profile]

List large regions.

Print region and total population for those regions with a total population of at least 100 million.

Use 35 characters for the region name and 13 characters for the total population.


[Font] [Default] [Show] [Resize] [History] [Profile]