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 ?
You can use
Map#entrySetmethod, if you want to access thekeysandvaluesparallely from yourHashMap: –Also, you can override
toStringmethod in yourRecordclass, to get String Representation of yourinstanceswhen you print them infor-eachloop.UPDATE: –
If you want to sort your
Mapon the basis ofkeyin alphabetical order, you can convert yourMaptoTreeMap. It will automatically put entries sorted by keys: –For more detailed explanation, see this post: – how to sort Map values by key in Java