Iterate over HashMap in Java

I am trying to create a Cholorpleth map using Processing and geoJSON. It’s hard for me to go through mine HashMapto check a property that will determine the color intensity of polygons.

This is currently my code:

void draw() {
  map.draw();
  for (int i = 0; i < countyMarkers.size(); i++) {
   Marker county = countyMarkers.get(i);
   String pop = countyData.get("pop").toString();
   println(i + "pop: " + pop);   
}

What I would like to do is loop through mine HashMap countyDataand get the totality of each entry. Use the if statement to compare it with a value and then give it a color based on a statement that is true.

My HashMap can be seen here:

{name=Carlow, pop=54,612}
{name=Cavan, pop=73,183}
{name=Clare, pop=117,196}
{name=Cork, pop=519,032}
{name=Donegal, pop=161,137}
{name=Dublin, pop=1,273,069}
{name=Galway, pop=250,541}
{name=Kerry, pop=145,502}
{name=Kildare, pop=210,312}
{name=Kilkenny, pop=95,419}
{name=Laois, pop=80,559}
{name=Letrim, pop=31,796}
{name=Limerick, pop=191,809}
{name=Longford, pop=39,000}
{name=Louth, pop=122,897}
{name=Mayo, pop=130,638}
{name=Meath, pop=184,135}
{name=Monaghan, pop=60,483}
{name=Offaly, pop=76,687}
{name=Roscommon, pop=64,065}
{name=Sligo, pop=65,393}
{name=Tipperary, pop=158,754}
{name=Waterford, pop=113,795}
{name=Westmeath, pop=86,164}
{name=Wexford, pop=145,320}
{name=Wicklow, pop=136,640}

I am relatively new to working with this type of data, so I disagree with where I am going wrong. I think my logic is correct, I'm just not sure how to implement it. I would appreciate any help in this matter.

This line is currently → println(i + "pop: " + pop);

.

,

0pop: 136,640
1pop: 136,640
2pop: 136,640
3pop: 136,640
4pop: 136,640
5pop: 136,640
6pop: 136,640
7pop: 136,640
8pop: 136,640
9pop: 136,640
10pop: 136,640
11pop: 136,640
12pop: 136,640
13pop: 136,640
14pop: 136,640
15pop: 136,640
16pop: 136,640
17pop: 136,640
18pop: 136,640
19pop: 136,640
20pop: 136,640
21pop: 136,640
22pop: 136,640
23pop: 136,640
24pop: 136,640
25pop: 136,640

HashMap.

void getCountyDetails(List<Marker>m) {  
  countyData = new HashMap();
  for (Marker county: countyMarkers) {
    countyData.putAll(county.getProperties());
    println(countyData);
  }
}

    Set<String> keySet = countyData.keySet();
  for(String pop : keySet){
     String value = countyData.get(pop).toString();
     println("population:" + value);
   }

:

population:Wicklow
population:136,640
population:Wicklow
population:136,640
population:Wicklow
population:136,640
population:Wicklow
population:136,640
population:Wicklow
population:136,640
population:Wicklow
population:136,640
population:Wicklow
population:136,640
population:Wicklow
population:136,640
population:Wicklow
population:136,640

, - HashMap

HashMap

void getCountyDetails(List<Marker>m) {  
  countyData = new HashMap();
  int i = 0;
  for (Marker county: countyMarkers) {
    countyData.put(i,county.getProperties());
    i++;
  }
}
+3
3

, countyData.get("pop") - key, - value, key.

PS: , keySet:

Set<KeyClass> keySet = myMap.keySet();
for(KeyClass key : keySet){
     ValueClass value = myMap.get(key);
     //do stuff
 }

, putAll :

( ). put (k, v) k v

+2
 for (Map.Entry<String, Integer> countyMarkers: map.entrySet())
    {
        System.out.println(countyMarkers.getKey() + " " + countyMarkers.getValue());
    }

entrySet(), Set , , getKey() getValue()

+1

The problem is this:

String pop = countyData.get("pop").toString();

you hard copy the key to get "pop"

you probably want:

 String pop = countyData.get(county).toString();
0
source

All Articles