11

Possible Duplicate:
How do I iterate over each Entry in a Map?

I am having a MAP, Map<String, Records> map = new HashMap<String, Records> ();

public class Records 
{
    String countryName;
    long numberOfDays;

    public String getCountryName() {
        return countryName;
    }
    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }
    public long getNumberOfDays() {
        return numberOfDays;
    }
    public void setNumberOfDays(long numberOfDays) {
        this.numberOfDays = numberOfDays;
    }

    public Records(long days,String cName)
    {
        numberOfDays=days;
        countryName=cName;
    }

    public Records()
    {
        this.countryName=countryName;
        this.numberOfDays=numberOfDays;
    }

I have implemented the methods for map, now please tell me How do I access all the values that are present in the hashmap. I need to show them on UI in android ?

Community
  • 1
  • 1
  • What error you come across? You should able to get the record by the key you assigned for the record or you can iterate it by foreach loop. – Drogba Oct 22 '12 at 08:50

4 Answers4

6

You can do it by using for loop

Set keys = map.keySet();   // It will return you all the keys in Map in the form of the Set


for (Iterator i = keys.iterator(); i.hasNext();) 
{

      String key = (String) i.next();

      Records value = (Records) map.get(key); // Here is an Individual Record in your HashMap
}
Edward
  • 1,367
  • 8
  • 26
  • 43
M.Mohsin
  • 314
  • 1
  • 3
  • 15
3

You can use Map#entrySet method, if you want to access the keys and values parallely from your HashMap: -

Map<String, Records> map = new HashMap<String, Records> ();

//Populate HashMap

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

Also, you can override toString method in your Record class, to get String Representation of your instances when you print them in for-each loop.

UPDATE: -

If you want to sort your Map on the basis of key in alphabetical order, you can convert your Map to TreeMap. It will automatically put entries sorted by keys: -

    Map<String, Integer> treeMap = new TreeMap<String, Integer>(map);

    for(Map.Entry<String, Integer> entry: treeMap.entrySet()) {
        System.out.println(entry.getKey() + " : " + entry.getValue());

    }

For more detailed explanation, see this post: - how to sort Map values by key in Java

Community
  • 1
  • 1
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • Is there any way by which I can sort keys are per alphabetical order ? –  Oct 22 '12 at 09:02
  • 1
    @onkar. You can use `TreeMap` for that case. See this post: - http://stackoverflow.com/questions/922528/how-to-sort-map-values-by-key-in-java – Rohit Jain Oct 22 '12 at 09:08
0

map.values() gives you a Collection with all the values in your HashMap.

Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25
guido.rota
  • 104
  • 4
0

If you are having HashMap ready with data, then you just need to iterate over HashMap keys. Just implement an iteration and fetch data one by one.

Check this: Iterate through a HashMap

Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295