I've run into the same problem a lot of people seem to face coming from PHP, the lack of a decent and easy to use associative array solution. I've read quesions here which basically all suggested to use a HashMap, like this Q: Java associative-array
However, I don't think the solutions mentioned will solve my problem. I'll explain.
I have a list with 250 items (countries) for which I want to store data. The data is of undefined length, meaning it can hold multiple entries per "column", sometimes no entry, sometimes 4, etcetera.
in PHP I could just do this:
$country_data = new array();
$country_data["nl"]["currency"] = "euro";
$country_data["nl"]["languages"] = "Dutch";
...
$country_data["us"]["currency"] = "US dollar";
$country_data["us"]["languages"] = array("English", "Spanish");
So sometimes I want to store an array, sometimes not. Ofcourse it could also be a array with only one entry instead of a string, but I'm just saying.
So, the question is, how do I store and fetch arrays in arrays in a HashMap? I understand I am pretty much stuck with the ugly HashMap solution, but I still can't see how this will let me store arrays, I'm sure I'm overlooking something simple. An example based on mine would be great!
UPDATE
I opted to go for HashMaps of HashMaps The reason for this I need to be able to oversee everything easily, and change a few lines of values when needed. And this is flexible, I can easily just get a country name based on country code, a language, or I can get the country_data HashMap when I need it, or all country names, etcetera.
public class iso_countries {
Map<String, Object> country_data = new HashMap<String, Object>();
Map<String, String> country_name = new HashMap<String, String>();
Map<String, String[]> country_idd = new HashMap<String, String[]>();
Map<String, String[]> country_cid = new HashMap<String, String[]>();
public iso_countries(){
country_name.put("nl", "Netherlands");
country_idd.put("nl", new String[]{"+31"});
country_cid.put("nl", new String[]{"#31#", "*31#"});
setData(country_name, country_cid, country_idd);
// 249 * 4 lines more and later...
}//end method
public void setData(Map country_name, Map country_cid, Map country_idd){
country_data.put("name", country_name);
country_data.put("idd", country_idd);
country_data.put("cid", country_cid);
}//end method
public String getCountryName(String countryCode){
String name = country_name.get(countryCode);
return name;
}//end method
public String[] getCountryIdd(String countryCode){
String prefix[] = country_idd.get(countryCode);
return prefix;
}//end method
public String[] getCountryCid(String countryCode){
String cid[] = country_cid.get(countryCode);
return cid;
}//end method
}//end class